note 8338 added to Date and Time functions

From: Date: Wed, 06 Sep 2000 23:33:27 +0000
Subject: note 8338 added to Date and Time functions
Groups: php.notes 
Request: Send a blank email to php-notes+get-4269@lists.php.net to get a copy of this message
Below is a function I adapted from an excellent article by Allan Kent on PHP date/time. The full article can be found at www.phpbuilder.com/columns/ This function will add or subtract time from a given unix timestamp, based on an interval you select (i.e., month, day, seconds, etc). <?php /* Variables for use in the "$interval" argument //Y year //quarter Quarter //m Month //day day //week Week of year //H Hour //i Minute //s Second */ function time_add ($interval, $number, $date) { $date_time_array = getdate($date); $hours = $date_time_array["hours"]; $minutes = $date_time_array["minutes"]; $seconds = $date_time_array["seconds"]; $month = $date_time_array["mon"]; $day = $date_time_array["mday"]; $year = $date_time_array["year"]; switch ($interval) { case "Y": $year +=$number; break; case "quarter": $month +=($number*3); break; case "m": $month +=$number; break; case "d": $day+=$number; break; case "week": $day+=($number*7); break; case "H": $hours+=$number; break; case "i": $minutes+=$number; break; case "s": $seconds+=$number; break; } $result_time = mktime($hours, $minutes, $seconds, $month, $day, $year); return $result_time; } //To convert the date March 1, 2000 15:51:57 into a unix timestamp $today = mktime(15, 51, 57, 3, 1, 2000); //To calculate the unix timstamp for one day before "$today" $one_day_ago = time_add ("d", -1, $today); //To convert $four_days_ago into something readable by a human $human_readable = date("Y-m-d H:i:s", $one_day_ago); echo $human_readable; ?> http://www.php.net/manual/ref.datetime.php

« previous php.notes (#4269) next »