Jump to content


Photo

Finding the difference between 2 timestamps


  • Please log in to reply
9 replies to this topic

#1 gladiator83x

gladiator83x
  • Members
  • PipPipPip
  • Advanced Member
  • 45 posts

Posted 24 July 2006 - 04:24 PM

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?

#2 Joe Haley

Joe Haley
  • Members
  • PipPipPip
  • Advanced Member
  • 103 posts
  • LocationCanada, eh?

Posted 24 July 2006 - 04:33 PM

2004-10-20 15:33:12
0123456789012345678

<?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
?>

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 String, not Integer.
Give a man a fish; you have fed him for today.  Teach a man to fish; and you have fed him for a lifetime
Don't teach men to program. Teach them to fish.

Please, try the RTFM solution before asking for help:
http://php.net/manual/en/index.php

#3 redarrow

redarrow
  • Members
  • PipPipPip
  • Advanced Member
  • 7,308 posts
  • Locationlondon

Posted 24 July 2006 - 04:43 PM

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.
Wish i new all about php DAM i will have to learn
((EMAIL CODE THAT WORKS))
http://simpleforum.ath.cx/mail2.inc
((PAYPAL INTEGRATION THAT WORKS))
http://simpleforum.a...aypal1_info.inc

#4 gladiator83x

gladiator83x
  • Members
  • PipPipPip
  • Advanced Member
  • 45 posts

Posted 24 July 2006 - 04:52 PM

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;

#5 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 24 July 2006 - 04:56 PM

The strtotime() function returns the number of seconds since 1-1-1970.

try:
<?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";
?>

Ken

#6 gladiator83x

gladiator83x
  • Members
  • PipPipPip
  • Advanced Member
  • 45 posts

Posted 24 July 2006 - 05:05 PM

Thanks so much all of you that replied!!! It works like a charm now  8)

#7 redarrow

redarrow
  • Members
  • PipPipPip
  • Advanced Member
  • 7,308 posts
  • Locationlondon

Posted 24 July 2006 - 06:15 PM

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;
Wish i new all about php DAM i will have to learn
((EMAIL CODE THAT WORKS))
http://simpleforum.ath.cx/mail2.inc
((PAYPAL INTEGRATION THAT WORKS))
http://simpleforum.a...aypal1_info.inc

#8 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 24 July 2006 - 06:22 PM

How did you figure those numbers? The first two are fine, the last two are completely wrong.

<?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>';
?>

Ken

#9 redarrow

redarrow
  • Members
  • PipPipPip
  • Advanced Member
  • 7,308 posts
  • Locationlondon

Posted 24 July 2006 - 06:24 PM

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;



Wish i new all about php DAM i will have to learn
((EMAIL CODE THAT WORKS))
http://simpleforum.ath.cx/mail2.inc
((PAYPAL INTEGRATION THAT WORKS))
http://simpleforum.a...aypal1_info.inc

#10 redarrow

redarrow
  • Members
  • PipPipPip
  • Advanced Member
  • 7,308 posts
  • Locationlondon

Posted 24 July 2006 - 06:40 PM

ken i am sure ive seen you do the above code result with strtotime)/60* ect ect....

i want to lern lol ......................
Wish i new all about php DAM i will have to learn
((EMAIL CODE THAT WORKS))
http://simpleforum.ath.cx/mail2.inc
((PAYPAL INTEGRATION THAT WORKS))
http://simpleforum.a...aypal1_info.inc




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users