开发者

PHP Get 1st weekday

I have tried to find the 1st weekday from a specified date, but I have not figured out any solution, but I am thinking, that you can do something like strtotime("-x days", $time)

Just now I am using this method to calculate how many days to go back, but I want to know, if th开发者_高级运维is can be optimized in any way

if ($i == 0)
   return 6;
else
   return $i-1;

Where $i is the numeric representation of the day of the week (same as PHP's date('w'))

I also need to find the last day of a week, but there I think, you can do

strtotime('+'.date('w', $dateTo).'days', $dateTo);


That's all relative date math. If you've got a proper PHP timestamp, then it's just a simple integer. date('w') is the proper method for extracting a day-of-week value from that timestamp, but the rest can be done with regular math:

$today = date('w', $dateTo);
$last_sunday = $dateTo - (86400 * $today); // same time on previous sunday
$last_day_of_week = $dateTo + (86400 * (6 % $today));

This is faster than round-tripping everything through strtotime() and incurring the date string parsing penalties.


Try using strtotime() with a relative format. Something like this should work: strtotime('monday', $relativeTime); for the first weekday and strtotime('friday', $relativeTime); for the last weekday.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜