note 107536 added to function.date

From: Date: Wed, 15 Feb 2012 22:07:16 +0000
Subject: note 107536 added to function.date
Groups: php.notes 
Request: Send a blank email to php-notes+get-186158@lists.php.net to get a copy of this message
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); ?> ---- Server IP: 69.147.83.197 Probable Submitter: 68.179.14.105 ---- Manual Page -- http://www.php.net/manual/en/function.date.php Edit -- https://master.php.net/note/edit/107536 Del: integrated -- https://master.php.net/note/delete/107536/integrated Del: useless -- https://master.php.net/note/delete/107536/useless Del: bad code -- https://master.php.net/note/delete/107536/bad+code Del: spam -- https://master.php.net/note/delete/107536/spam Del: non-english -- https://master.php.net/note/delete/107536/non-english Del: in docs -- https://master.php.net/note/delete/107536/in+docs Del: other reasons-- https://master.php.net/note/delete/107536 Reject -- https://master.php.net/note/reject/107536 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#186158) next »