note 113012 added to function.date-add

From: Date: Sun, 18 Aug 2013 05:41:20 +0000
Subject: note 113012 added to function.date-add
Groups: php.notes 
Request: Send a blank email to php-notes+get-195999@lists.php.net to get a copy of this message
<?php function mysql_date_add($now = null, $adjustment ) { // normal mysql format is: date_add(now(), INTERVAL 1 MONTH) // its close to the strtotime() format, but we need to make a few adjustments // first we lowercase everything, not sure if this is needed but it seems // to be both mysql conventions to be capitalized and php to lowercase this, so // i follow suit. $adjustment = strtolower($adjustment); // next we want to get rid of the INTERVAL part, as neither it nor a corrisponding // keyword used in the strtotime() function. remmeber its lowercase now. $adjustment = str_replace('interval', '', $adjustment); // now the adjustment is suppsoed to have a + or - next to it to indicate direction // since strtotime() can be used to go both ways. We want to tack this one, but first // strip any white space off the begining of the $adjustment so we dont wind up with like // + 1 when we need +1 $adjustment = '+' . trim($adjustment); // we should now be left with something like '+1 month' which is valid strtotime() syntax! // next we need to handle the $now, normally people would pass now() if they want the current // time or a datetime/timestamp. We will need to account for this as well, we also // want to make use of having a default to now() type of behavior. we want to also // trim and lowercase what they send us just to make it easier to compair to if (is_null($now) || strtolower(trim($now)) == 'now()') { // defaulted to or requested a the current time $now = time(); } else { // here we are splitting out each part of the mysql timestamp , and storing it in the $parts array preg_match('/([0-9]{4})-([0-9]{2})-([0-9]{2}) ([0-9]{2}):([0-9]{2}):([0-9]{2})/', $now, $parts); // now we use each of the parts to generate a timestamp from it $now = mktime($parts[4], $parts[5], $parts[6], $parts[2], $parts[3], $parts[1]); } // now we finally call strtotime() with the properly formatted text and get the date/time // calculates done. I specify its returned as an integer to make things play more nicely // with eachother in case the conversion fails. $timestamp = (integer)strtotime($adjustment, $now); // finally we have the timestamp of the adjusted date nowe we just convert it back to the mysql // format and send it back to them. return date('Y-m-d H:i:s', $timestamp); } ?> ---- Server IP: 69.147.83.199 Probable Submitter: 70.15.147.111 ---- Manual Page -- http://php.net/manual/en/function.date-add.php Edit -- https://master.php.net/note/edit/113012 Del: integrated -- https://master.php.net/note/delete/113012/integrated Del: useless -- https://master.php.net/note/delete/113012/useless Del: bad code -- https://master.php.net/note/delete/113012/bad+code Del: spam -- https://master.php.net/note/delete/113012/spam Del: non-english -- https://master.php.net/note/delete/113012/non-english Del: in docs -- https://master.php.net/note/delete/113012/in+docs Del: other reasons-- https://master.php.net/note/delete/113012 Reject -- https://master.php.net/note/reject/113012 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#195999) next »