note 116201 deleted from function.strtotime by salathe
| From: | salathe@php.net | Date: | Fri, 21 Nov 2014 15:00:37 +0000 |
| Subject: | note 116201 deleted from function.strtotime by salathe | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-200996@lists.php.net to get a copy of this message | ||
Note Submitter: i at blackrabbit dot info
----
With combining strtotime() with date() it's easy to out date-related posts with human-readable
ranges: today, yesterday, on this month, on previous month, etc..
$today = date('Y-m-d', $date);
$yesterday = date('Y-m-d', strtotime('Yesterday', $date));
$this_week_start = date('N', $date) != 1 ? date('Y-m-d',
strtotime('previous Monday', $date)) : $today;
$have_this_week = date('N', $date) > 2;
$this_week_end = $have_this_week ?
date('Y-m-d', strtotime('previous Day', strtotime($yesterday))) :
date('Y-m-d', strtotime('this Sunday', $date));
$prev_week_start = date('Y-m-d', strtotime('previous Monday',
strtotime($this_week_start)));
$prev_week_end = date('Y-m-d', strtotime('previous Sunday',
strtotime($this_week_start)));
if(strtotime($prev_week_end) >= strtotime($yesterday))
$prev_week_end = date('Y-m-d', strtotime('previous Day',
strtotime($yesterday)));
$this_month_start = date('Y-m-01', $date);
$have_this_month = strtotime($this_month_start) < strtotime($prev_week_start);
$this_month_end = $have_this_month ?
date('Y-m-d', strtotime('previous Day', strtotime($prev_week_start))) :
date('Y-m-d', strtotime('first Day of next Month', $date) - 1);
$prev_month_start = date('Y-m-01', strtotime('previous Month', $date));
$prev_month_end = date('Y-m-d', strtotime('first Day of this Month', $date) -
1);
if(strtotime($prev_month_end) >= strtotime($prev_week_start))
$prev_month_end = date('Y-m-d', strtotime($prev_week_start) - 1);
echo 'Today: ', $today, ENDL;
echo 'Yesterday: ', $yesterday, ENDL;
if($have_this_week)
{
echo 'Earlier on this week start: ', $this_week_start, ENDL;
echo 'Earlier on this week end: ', $this_week_end, ENDL;
}
echo 'On previous week start: ', $prev_week_start, ENDL;
echo 'On previous week end: ', $prev_week_end, ENDL;
if($have_this_month)
{
echo 'Earlier on this month start: ', $this_month_start, ENDL;
echo 'Earlier on this month end: ', $this_month_end, ENDL;
}
echo ($have_this_month ? 'On ' : 'Earlier on '), 'previous month start:
', $prev_month_start, ENDL;
echo ($have_this_month ? 'On ' : 'Earlier on '), 'previous month end:
', $prev_month_end, ENDL;