gladiator83x Posted July 24, 2006 Share Posted July 24, 2006 I have 2 timestamps of the following format: 2004-10-20 15:33:12 & 2006-07-19 10:06:28 . Is there a way that I can find out the difference in days between these 2 timestamps? Quote Link to comment https://forums.phpfreaks.com/topic/15509-finding-the-difference-between-2-timestamps/ Share on other sites More sharing options...
Joe Haley Posted July 24, 2006 Share Posted July 24, 2006 [code]2004-10-20 15:33:120123456789012345678[/code][code=php:0]<?php$a = substr($date1, 5, 2); //days (or is it months? i suck with date formats) of date 1$b = substr($date2, 5, 2); //days (months?) of date 2?>[/code]FYI: The key in the code block shows you the 'number' of charicters. '10-20' starts at offset 5, and has a length of 5.Also, the 'type' of $a and $b is [u]String[/u], [b]not[/b] [u]Integer[/u]. Quote Link to comment https://forums.phpfreaks.com/topic/15509-finding-the-difference-between-2-timestamps/#findComment-62958 Share on other sites More sharing options...
redarrow Posted July 24, 2006 Share Posted July 24, 2006 look up strtotimelinkhttp://uk.php.net/strtotimeexample only but check okput the dates in a friendly format selecting day.$date1=strtotime("d",$what_ever_date_form_database);$date2=strtotime("d",$what_ever_date_from_database);$date_result=$date1-$date2;echo $date_result;not sure somethink like that. Quote Link to comment https://forums.phpfreaks.com/topic/15509-finding-the-difference-between-2-timestamps/#findComment-62974 Share on other sites More sharing options...
gladiator83x Posted July 24, 2006 Author Share Posted July 24, 2006 My output doesnt return an error, so thats good. It just doesn't return anything. I tried the strtotime() function as well as the substr() function. Any idea what I am doing wrong? $col_val and $creation time return the time when I echo them out--It just seems like I cannot convert them into another form.$date1=strtotime("d-m-y",$col_val);$date2=strtotime("d-m-y",$creation_ts);echo $date1;$date_result=$date1-$date2;echo $date_result; Quote Link to comment https://forums.phpfreaks.com/topic/15509-finding-the-difference-between-2-timestamps/#findComment-62983 Share on other sites More sharing options...
kenrbnsn Posted July 24, 2006 Share Posted July 24, 2006 The strtotime() function returns the number of seconds since 1-1-1970.try:[code]<?php$date1=strtotime($col_val);$date2=strtotime($creation_ts);echo $date1;$date_result=$date1-$date2;echo $date_result;$num_days = floor($date_result/86400); // 86400 is the number of seconds in a dayecho "<br>Days: $num_days";?>[/code]Ken Quote Link to comment https://forums.phpfreaks.com/topic/15509-finding-the-difference-between-2-timestamps/#findComment-62984 Share on other sites More sharing options...
gladiator83x Posted July 24, 2006 Author Share Posted July 24, 2006 Thanks so much all of you that replied!!! It works like a charm now 8) Quote Link to comment https://forums.phpfreaks.com/topic/15509-finding-the-difference-between-2-timestamps/#findComment-62989 Share on other sites More sharing options...
redarrow Posted July 24, 2006 Share Posted July 24, 2006 i like the code but can not work the secons out am i going mad cheers.$sixty_secons_to_min=60;$one_hour_secons=3600;$twelve_hours_secons=439200;$twenty_four_hours_secons=10540800; Quote Link to comment https://forums.phpfreaks.com/topic/15509-finding-the-difference-between-2-timestamps/#findComment-63031 Share on other sites More sharing options...
kenrbnsn Posted July 24, 2006 Share Posted July 24, 2006 How did you figure those numbers? The first two are fine, the last two are completely wrong.[code]<?php$secs['secs_in_min'] = 60;$secs['secs_in_hour'] = 60 * $secs['secs_in_min'];$secs['secs_in_12hour'] = $secs['secs_in_hour'] * 12;$secs['secs_in_day'] = $secs['secs_in_hour'] * 24;echo '<pre>' . print_r($secs,true) . '</pre>';?>[/code]Ken Quote Link to comment https://forums.phpfreaks.com/topic/15509-finding-the-difference-between-2-timestamps/#findComment-63036 Share on other sites More sharing options...
redarrow Posted July 24, 2006 Share Posted July 24, 2006 soory ken mateOkay, here we go! There are 60 seconds in a minute, and 60 minutes in an hour. So there are 60 x 60 seconds in an hour, in other words 3600 seconds in an hour. Furthermore, there are 24 hours in a day, so there are 24 x 3600 seconds = 86400 seconds in a day. Also, there are 365 days in a year, so there are 365 x 86400 seconds = 31,536,000 seconds in a year. Let's summarize this in a table:1 hour = 3600 seconds1 day = 86400 seconds1 year = 31,536,000 seconds$sixty_secons_to_min=60;$one_hour_secons=3600;$twelve_hours_secons=43200;$twenty_four_hours_secons=86400; Quote Link to comment https://forums.phpfreaks.com/topic/15509-finding-the-difference-between-2-timestamps/#findComment-63038 Share on other sites More sharing options...
redarrow Posted July 24, 2006 Share Posted July 24, 2006 ken i am sure ive seen you do the above code result with strtotime)/60* ect ect....i want to lern lol ...................... Quote Link to comment https://forums.phpfreaks.com/topic/15509-finding-the-difference-between-2-timestamps/#findComment-63053 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.