note 80693 modified in function.strtotime by danbrown

From: Date: Sun, 20 Jul 2008 04:04:33 +0000
Subject: note 80693 modified in function.strtotime by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-142108@lists.php.net to get a copy of this message
This code is for relative dates similar to the add date functionality that C# has. It allows for negative values to substract dates. <?php /*pre: format is either 'd' for days,'m' for months or 'y' for years length is the number of days/months/years date_array is optional,format is getdate() post: returns the integer value of the date with the added length */ function date_add($length,$format,$date_array=null){ $new_timestamp = -1; $date = is_null($date_array)?getdate():$date_array; switch(strtolower($format)){ case 'd': $new_timestamp = mktime(0,0,0,$date["mon"],$date["mday"]+$length,$date["year"]); break; case 'm': $new_timestamp = mktime(0,0,0,$date["mon"]+$length,$date["mday"],$date["year"]); break; case 'y': $new_timestamp = mktime(0,0,0,$date["mon"],$date["mday"],$date["year"]+$length); break; default: break; } return $new_timestamp; } ?> --was-- This code is for relative dates similar to the add date functionality that C# has. It allows for negative values to substract dates. /*pre: format is either 'd' for days,'m' for months or 'y' for years length is the number of days/months/years date_array is optional,format is getdate() post: returns the integer value of the date with the added length */ function date_add($length,$format,$date_array=null){ $new_timestamp = -1; $date = is_null($date_array)?getdate():$date_array; switch(strtolower($format)){ case 'd': $new_timestamp = mktime(0,0,0,$date["mon"],$date["mday"]+$length,$date["year"]); break; case 'm': $new_timestamp = mktime(0,0,0,$date["mon"]+$length,$date["mday"],$date["year"]); break; case 'y': $new_timestamp = mktime(0,0,0,$date["mon"],$date["mday"],$date["year"]+$length); break; default: break; } return $new_timestamp; } http://php.net/manual/en/function.strtotime.php

« previous php.notes (#142108) next »