Re: subtracting time from date and time

From: Date: Mon, 18 Jun 2007 19:21:21 +0000
Subject: Re: subtracting time from date and time
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-257332@lists.php.net to get a copy of this message
Richard Kurth wrote:
-----Original Message----- From: Chris Boget [mailto:chris.boget@wild.net] Sent: Monday, June 18, 2007 10:55 AM To: Brad Bonkoski; Richard Kurth Cc: php-general@lists.php.net Subject: Re: [PHP] subtracting time from date and time
Something like this will get it into a time stamp...and
then you can
do your calculations with ease, and reformat... <?php
       $str = "20070617T193500";
       list($date, $time) = explode("T",$str); ?>
Even easier: $timestamp = strtotime( $str ); http://us2.php.net/manual/en/function.strtotime.php $oneMinute = 60; // seconds
$oneHour    = $oneMinute * 60;
$oneDay     = $oneHour * 24;
echo '5 minutes ago: ' . date( 'm/d/Y h:i:s', $timestamp - ( $oneMinute * 5 )); echo '5 hours ago: ' . date( 'm/d/Y h:i:s', $timestamp - ( $oneHour * 5 )); echo '5 days ago: ' . date( 'm/d/Y h:i:s', $timestamp - ( $oneDay * 5 ));
This works great tell you get to 8 hours ago it shows the correct time but it does not change the date to the day before. 8 hours ago should be 06/16/2007 11:35:00 but what it shows is 06/17/2007 11:35:00 $str = "20070617T193500";
Notice that this time stamp is formatted in 24/hr days. so year 2007 month 6 day 17 hour 19 = 7pm (not 7am) minute 15 second 00
$timestamp = strtotime( $str ); $oneMinute = 60; // seconds
$oneHour    = $oneMinute * 60;
$oneDay     = $oneHour * 24;
echo 'time: ' . date( 'm/d/Y h:i:s', $timestamp ); echo"<br>"; echo '5 minutes ago: ' . date( 'm/d/Y h:i:s', $timestamp - ( $oneMinute *5 )); echo"<br>"; echo '15 minutes ago: ' . date( 'm/d/Y h:i:s', $timestamp - ( $oneMinute *15 )); echo"<br>"; echo '30 minutes ago: ' . date( 'm/d/Y h:i:s', $timestamp - ( $oneMinute *30 )); echo"<br>"; echo '1 hours ago: ' . date( 'm/d/Y h:i:s', $timestamp - ( $oneHour * 1 )); echo"<br>"; echo '2 hours ago: ' . date( 'm/d/Y h:i:s', $timestamp - ( $oneHour * 2 )); echo"<br>"; echo '5 hours ago: ' . date( 'm/d/Y h:i:s', $timestamp - ( $oneHour * 5 )); echo"<br>"; echo '7 hours ago: ' . date( 'm/d/Y h:i:s', $timestamp - ( $oneHour * 7 )); echo"<br>"; echo '8 hours ago: ' . date( 'm/d/Y h:i:s', $timestamp - ( $oneHour * 8));
-- Jim Lucas "Some men are born to greatness, some achieve greatness, and some have greatness thrust upon them." Twelfth Night, Act II, Scene V by William Shakespeare

« previous php.general (#257332) next »