note 76252 added to function.strtotime

From: Date: Sat, 07 Jul 2007 17:47:32 +0000
Subject: note 76252 added to function.strtotime
Groups: php.notes 
Request: Send a blank email to php-notes+get-128286@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). 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... ---- Server IP: 128.39.198.38 Probable Submitter: 80.203.100.213 ---- Manual Page -- http://www.php.net/manual/en/function.strtotime.php Edit -- https://master.php.net/note/edit/76252 Del: integrated -- https://master.php.net/note/delete/76252/integrated Del: useless -- https://master.php.net/note/delete/76252/useless Del: bad code -- https://master.php.net/note/delete/76252/bad+code Del: spam -- https://master.php.net/note/delete/76252/spam Del: non-english -- https://master.php.net/note/delete/76252/non-english Del: in docs -- https://master.php.net/note/delete/76252/in+docs Del: other reasons-- https://master.php.net/note/delete/76252 Reject -- https://master.php.net/note/reject/76252 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#128286) next »