note 62293 deleted from function.mktime by mazzanet

From: Date: Fri, 24 Feb 2006 08:49:41 +0000
Subject: note 62293 deleted from function.mktime by mazzanet
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-104361@lists.php.net to get a copy of this message
Note Submitter: xenlab at gmail dot com ---- A bit of code to get 'last friday': <?php $day = date("w",time()); switch ($day) { // sunday case 0: $last_friday = mktime(12,0,0,date("m"),date("d")-2,date("Y")); break; // monday case 1: $last_friday = mktime(12,0,0,date("m"),date("d")-3,date("Y")); break; // tuesday case 2: $last_friday = mktime(12,0,0,date("m"),date("d")-4,date("Y")); break; // wednesday case 3: $last_friday = mktime(12,0,0,date("m"),date("d")-5,date("Y")); break; // thursday case 4: $last_friday = mktime(12,0,0,date("m"),date("d")-6,date("Y")); break; // friday case 5: $last_friday = mktime(12,0,0,date("m"),date("d")-7,date("Y")); break; // saturday case 6: $last_friday = mktime(12,0,0,date("m"),date("d")-1,date("Y")); break; } ?> to make it work for another day of the week, just shift the date("d")-7 part up and change the corresponding ones to match... example: if you wanted last thursday instead, thursday would become date("d")-7, friday date("d")-1, saturday date("d")-2, sunday date("d")-3, etc. etc. Hope this snippets helps someone, as I didn't see a very straight forward example of this kind of logic on any of the date(), mktime(), or time() pages. Have fun!

« previous php.notes (#104361) next »