note 76252 deleted from function.strtotime by levim
| From: | levim@php.net | Date: | Fri, 12 Jul 2013 16:36:33 +0000 |
| Subject: | note 76252 deleted from function.strtotime by levim | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-195593@lists.php.net to get a copy of this message | ||
Note Submitter: olav dot morkrid at gmail dot com
----
when using strtotime("wednesday"), you will get different results whether you ask before
or after wednesday, since strtotime always looks ahead to the *next* weekday.
strtotime() does not seem to support forms like "this wednesday", "wednesday this
week", etc.
the following function addresses this by always returns the same specific weekday (1st argument)
within the *same* week as a particular date (2nd argument).
<?php
function weekday($day="", $now="") {
$now = $now ? $now : "now";
$day = $day ? $day : "now";
$rel = date("N", strtotime($day)) - date("N");
$time = strtotime("$rel days", strtotime($now));
return date("Y-m-d", $time);
}
?>
example use:
weekday("wednesday"); // returns wednesday of this week
weekday("monday, "-1 week"); // return monday the in previous week
ps! the ? : statements are included because strtotime("") without gives 1 january 1970
rather than the current time which in my opinion would be more intuitive...