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 11-24-2010, 04:11 PM   PM User | #1
ClubCosmic
Regular Coder

 
Join Date: May 2004
Posts: 144
Thanks: 0
Thanked 0 Times in 0 Posts
ClubCosmic is an unknown quantity at this point
php date time functions

Hi,
I have the following date time format:
24-11-2010 11:6:57

I want to get a beginning datetime and an ending datetime and caluculate the difference in hours. How can i transform the above date to a format that can caluslte? the stamp posted is from a calendar date picker.

thanks in advance
c.c.
ClubCosmic is offline   Reply With Quote
Old 11-24-2010, 04:20 PM   PM User | #2
mlseim
Master Coder

 
mlseim's Avatar
 
Join Date: Jun 2003
Location: Cottage Grove, Minnesota
Posts: 9,045
Thanks: 8
Thanked 1,029 Times in 1,020 Posts
mlseim has a spectacular aura aboutmlseim has a spectacular aura aboutmlseim has a spectacular aura about
Change it to unix timestamp ....

http://php.net/manual/en/function.strtotime.php

The timestamp resolution is in seconds.
mlseim is offline   Reply With Quote
Old 11-24-2010, 04:38 PM   PM User | #3
ClubCosmic
Regular Coder

 
Join Date: May 2004
Posts: 144
Thanks: 0
Thanked 0 Times in 0 Posts
ClubCosmic is an unknown quantity at this point
and the above format is acceptable so i dont have to change it?

thanks
ClubCosmic is offline   Reply With Quote
Old 11-24-2010, 05:52 PM   PM User | #4
mlseim
Master Coder

 
mlseim's Avatar
 
Join Date: Jun 2003
Location: Cottage Grove, Minnesota
Posts: 9,045
Thanks: 8
Thanked 1,029 Times in 1,020 Posts
mlseim has a spectacular aura aboutmlseim has a spectacular aura aboutmlseim has a spectacular aura about
Correct ... your format is fine.
The conversion (strtotime) is only for calculation.


Like this example:
PHP Code:
<?php

$time1
="24-11-2010 11:6:57";
$time2="24-11-2010 19:4:35";

// difference in seconds
$diff=abs(strtotime($time1)-strtotime($time2));

// difference in hours (rounded up).
echo intval($diff/3600);

?>
mlseim is offline   Reply With Quote
Old 01-25-2013, 10:16 AM   PM User | #5
nani_nisha06
Regular Coder

 
Join Date: Oct 2012
Location: mother land --india
Posts: 159
Thanks: 37
Thanked 2 Times in 2 Posts
nani_nisha06 is an unknown quantity at this point
Good solution above....These post help me lot...........Thanks
nani_nisha06 is offline   Reply With Quote
Old 01-25-2013, 02:52 PM   PM User | #6
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
I'm a fan of the dateinterval myself. Hours is a pain though, so for that I'd still suggest sticking with strtotime (or massage the DateInterval which would be more work). It's a bit easier to use overall IMO.
PHP Code:
$time1= new DateTime("24-11-2010 11:6:57");
$time2= new DateTime("24-11-2010 19:4:35");

$diDiff $time1->diff($time2); 
$diDiff is the daterInterval difference between the two. Best used for large dates, not small (it doesn't keep track of a total number of hours, only a total number of days).
PHP Code:
print('Difference is ' $diDiff->format('%y years, %m months, %d days, %h hours (total days: %a)')); 
The diff on dateTime also has an inversion parameter to always force absolute.

So with the smaller times, I would recommend you use simple subtraction (either using strtotime to get the integer, or by using DateTime and pulling the integer out) as indicated here. For anything larger, I'd suggest the DateInterval (to see how many years, months, days, etc ago). The DateInterval doesn't play great with hours, but can be easily calculated based on the total number of days past.

And yes, I'm also aware that this thread is a couple of years old now, so the DateInterval would just *barely* have applied as it was released mid 2009.
__________________
As of PHP 5.5, the MySQL library has been officially deprecated. It is recommended to move to either MySQLi or PDO libraries for your mysql connectivity. See here for help choosing which interface you prefer: http://php.net/manual/en/mysqlinfo.api.choosing.php
Fou-Lu is offline   Reply With Quote
Old 01-25-2013, 11:04 PM   PM User | #7
mlseim
Master Coder

 
mlseim's Avatar
 
Join Date: Jun 2003
Location: Cottage Grove, Minnesota
Posts: 9,045
Thanks: 8
Thanked 1,029 Times in 1,020 Posts
mlseim has a spectacular aura aboutmlseim has a spectacular aura aboutmlseim has a spectacular aura about
Fou-Lu,

I'm pretty much blown away at how much trouble PHP has at doing date functions. There are dozens of date functions to pick from, 'use this one', 'use that one', 'don't use that unless you do this', etc. Make sure your dates are formatted like YYYY-MM-DD for more accurate calculations, etc. How does anyone know which method to use and when to use it?

What's happening? Does Perl, Java, C++, and all the others have the same issues with date/time calculations? Or is this something where PHP did it wrong from the beginning and now it has escalated into a whole fiasco of functions and tricks to make it work?
mlseim is offline   Reply With Quote
Old 01-26-2013, 05:19 AM   PM User | #8
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
IMO its something that the older PHP versions did wrong. Or its possible that there was no easy way to create backwards compatibility with the existing procedural code and so there was no option but to write another set of functions to handle them. Both are good possibilities.
DateTime is 5.x functionality. The entire library is unstable between 5.0 and 5.3 where many new classes and methods were added. So the only frustrating thing is writing something on 5.3 which just doesn't work with say 5.2.8.
strtotime is also instable though as indicated by the changelogs. <4.4, next was listed as +2, <5.0.2 now was incorrectly calculated from midnight, and < 5.1.0 returns -1 on failure instead of false which is just silly since -1 is a valid time.

Compared to other languages, its hard to say. Most other languages I use you import to do many of the time manipulations. Before namespace (and even since due to backwards compatibility), php is stuck with only global symbols so it has dozens of functions for date and time.
__________________
As of PHP 5.5, the MySQL library has been officially deprecated. It is recommended to move to either MySQLi or PDO libraries for your mysql connectivity. See here for help choosing which interface you prefer: http://php.net/manual/en/mysqlinfo.api.choosing.php
Fou-Lu is offline   Reply With Quote
Old 02-21-2013, 11:21 AM   PM User | #9
paulinetaylor85
New Coder

 
Join Date: Oct 2012
Posts: 13
Thanks: 0
Thanked 0 Times in 0 Posts
paulinetaylor85 is an unknown quantity at this point
Your mentioned date & time format is perfect. You can use this format any time.
paulinetaylor85 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:27 PM.


Advertisement
Log in to turn off these ads.