note 107536 deleted from function.date by peehaa
| From: | peehaa@php.net | Date: | Fri, 06 Jan 2017 15:57:49 +0000 |
| Subject: | note 107536 deleted from function.date by peehaa | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-208715@lists.php.net to get a copy of this message | ||
Note Submitter: frank at interactinet dot com
----
If you want to compare this week with the same week last year, here is some code to get you the time
at the beginning of the week. You can then add days, hours, etc to get to the day of the week that
you want to know about.
<?php
$time_passed = (date('N')-1)* 24 * 3600; // time since start of week in days
$startOfWeek = mktime(0,0,0,date('m'),date('d'),date('Y')) -
$time_passed;
$lastyear = $startOfWeek - 365*24*3600;
// make sure time used from last year is the same week of the year
$weekdiff = date('W') - date('W',$lastyear);
if($weekdiff != 0)
{
$lastyear = $lastyear + ($weekdiff*7*24*3600);
}
$lastyear_time_passed = (date('N',$lastyear)-1) * 24 * 3600; // time since start of week
in days
$startOfWeek_lastyear =
mktime(0,0,0,date('m',$lastyear),date('d',$lastyear),date('Y',$lastyear))
- $lastyear_time_passed;
?>
So now you have the unix time for the start of this week ($startOfWeek), and the start of the same
week last year ($startOfWeek_lastyear).
You can convert back to datetime format easily:
<?php
echo date('Y-m-d H:i:s',$startOfWeek).'<br>';
echo date('Y-m-d H:i:s',$startOfWeek_lastyear).'<br><br>';
echo date('l F jS, Y',$startOfWeek).'<br>';
echo date('l F jS, Y',$startOfWeek_lastyear);
?>