Jump to content

Archived

This topic is now archived and is closed to further replies.

gladiator83x

Finding the difference between 2 timestamps

Recommended Posts

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?

Share this post


Link to post
Share on other sites
[code]
2004-10-20 15:33:12
0123456789012345678[/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].

Share this post


Link to post
Share on other sites
look up strtotime

link
http://uk.php.net/strtotime

example only but check ok

put 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.

Share this post


Link to post
Share on other sites
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;

Share this post


Link to post
Share on other sites
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 day
echo "<br>Days: $num_days";
?>[/code]

Ken

Share this post


Link to post
Share on other sites
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;

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
soory ken mate


Okay, 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 seconds
1 day  = 86400 seconds
1 year = 31,536,000 seconds

$sixty_secons_to_min=60;

$one_hour_secons=3600;

$twelve_hours_secons=43200;

$twenty_four_hours_secons=86400;


Share this post


Link to post
Share on other sites
ken i am sure ive seen you do the above code result with strtotime)/60* ect ect....

i want to lern lol ......................

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.