Last active
November 30, 2016 14:45
-
-
Save YavorK/17e7c4bb67edfd40382638b669c8035e to your computer and use it in GitHub Desktop.
DateService.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class DateService | |
{ | |
/** | |
* Return number of day in the week in an European manner, | |
* 0 is Monday, 6 is Sunday | |
* @param $unixTimestamp integer | |
* @return integer | |
*/ | |
public function getDayOfWeekNumber($unixTimestamp) | |
{ | |
return date('N', $unixTimestamp) - 1; | |
} | |
/** | |
* This important since int he DB there is: year, week, day_of_week. | |
* To turn those into a unix timestamp use this function. | |
* @param $weekdayNumber integer 0 for Monday, 6 for Sunday | |
* @param $week integer number of the week | |
* @param $year integer number of the year | |
* @return integer unix timestamp | |
*/ | |
public function weekdaynumberWeekAndYearToTimestamp($weekdayNumber, $week, $year) | |
{ | |
$weekStartTime = $this->getWeekStartTimestamp($year, $week); | |
return $this->addDays($weekStartTime, $weekdayNumber); | |
} | |
/** | |
* @param $year integer number of the year | |
* @param $week integer number of the week | |
* @return integer unix timestamp | |
*/ | |
public function getWeekStartTimestamp($year, $week) | |
{ | |
$week = str_pad($week, 2, '0', STR_PAD_LEFT); | |
return strtotime($year . 'W' . $week); | |
} | |
/** | |
* @param $timestamp | |
* @param $days | |
* @return integer unix timestamp | |
*/ | |
public function addDaysToTimestamp($timestamp, $days) | |
{ | |
return $timestamp + 24 * 60 * 60 * $days; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment