Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 12-14-2012, 11:55 AM   PM User | #1
nickburrett
New Coder

 
Join Date: Jun 2009
Posts: 63
Thanks: 22
Thanked 0 Times in 0 Posts
nickburrett is an unknown quantity at this point
Adding time to a date and time?

Hello

I am trying to add 3 hours to a date and time, but when I do I get 3 hours and 12 minutes?

Can anyone explain what I am doing wrong?

PHP Code:
<?

$start_time 
"2012-12-11 12:00:00";
$hours_required "+3 hours";
$finish_calculation date('Y-m-d h:m:s'strtotime($start_time "+" $hours_required));

echo 
"<br>" $start_time;
echo 
"<br>" $hours_required;
echo 
"<br>" $finish_calculation;

?>
nickburrett is offline   Reply With Quote
Old 12-14-2012, 03:10 PM   PM User | #2
sunfighter
Senior Coder

 
Join Date: Jan 2011
Location: Missouri
Posts: 2,382
Thanks: 18
Thanked 350 Times in 349 Posts
sunfighter is on a distinguished road
I have always found working with date time a pain. PHP works in seconds not hours, months, or days. Php calculates the time in seconds from the epochs, January 1 1970 00:00:00 GMT, and goes from there.

Your answer, but you should look up why this works:
PHP Code:
<?php
$start_time 
= new DateTime('2012-12-11 12:00:00');
echo 
$start_time->format('Y-m-d H:i:s') . "<br />";

$finish_calculation $start_time->add(new DateInterval('PT3H'));
echo 
$finish_calculation->format('Y-m-d H:i:s') . "<br />";
?>
P.S. FYI you should always start php with <?php and not <?. The later has been discontinued and it will eventually bite you.
sunfighter is offline   Reply With Quote
Old 12-14-2012, 03:12 PM   PM User | #3
kbluhm
Senior Coder

 
kbluhm's Avatar
 
Join Date: Apr 2007
Location: Philadelphia, PA, USA
Posts: 1,502
Thanks: 2
Thanked 258 Times in 254 Posts
kbluhm will become famous soon enough
http://www.php.net/date

Lowercase m is the numeric month with leading zeros (12 == December)

Replace the lowercase m with a lowercase i... which is minutes with leading zeros.

strtotime() also takes a second argument, the start time, which always defaults to the current timestamp... so this will also work:
PHP Code:
$start_time "2012-12-11 12:00:00"
$hours_required "+3 hours"

$finish_calculation date'Y-m-d h:i:s'strtotime(
    
$hours_required,
    
strtotime$start_time )
) ); 
__________________
ZCE

Last edited by kbluhm; 12-14-2012 at 03:18 PM..
kbluhm is offline   Reply With Quote
Old 12-14-2012, 04:16 PM   PM User | #4
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,635
Thanks: 4
Thanked 2,448 Times in 2,417 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
Datetime ftw (5.3+):
PHP Code:
$dt = new DateTime($start_time);
$dt->add(new DateInterval('PT3H'));
print 
$dt->format('Y-m-d h:i:s'); 
As for PHP time being an integer, I'm not sure how many languages *don't* use integers to store time (including things like filesystem inodes). This is part of the issue with the year 2038 time problem. Of course by 2038 this won't be an issue as x86 will be a thing of the past.
Fou-Lu is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 05:01 PM.


Advertisement
Log in to turn off these ads.