note 76252 modified in function.strtotime by danbrown
| From: | danbrown@php.net | Date: | Sun, 20 Jul 2008 04:06:42 +0000 |
| Subject: | note 76252 modified in function.strtotime by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-142109@lists.php.net to get a copy of this message | ||
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...
--was--
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).
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...
http://php.net/manual/en/function.strtotime.php