Jump to content


Photo

Working with time


  • Please log in to reply
2 replies to this topic

#1 GingerRobot

GingerRobot
  • Staff Alumni
  • Advanced Member
  • 4,086 posts
  • LocationUK

Posted 02 August 2006 - 01:36 PM

Im looking for an easy way to find out how long has passed betweed two times that are in the mysql datetime format, for instance:
2006-08-02 01:01:37

I have made a function that explodes the string into its components and then uses mktime() to get a unix timestamp and then compare the 2 unix timestamps, but this seems long winded way of going about. Is there something more simple?


Here is the function i was using:
<?php
function to_time($datetime){
  $seperate = explode(" ",$datetime);
  $date = explode("-",$seperate[0]);
  $years = $date[0];
  $months = $date[1];
  $days = $date[2];
  $time = explode(":",$seperate[1]);
  $hours = $time[0];
  $minutes = $time[1];
  $seconds = $time[2];
  $timestamp = mktime($hours,$minutes,$seconds,$months,$days,$years);
	return $timestamp;		
}
?>

Any help would be much appreciated.

#2 ronverdonk

ronverdonk
  • Members
  • PipPipPip
  • Advanced Member
  • 277 posts
  • LocationNetherlands

Posted 02 August 2006 - 01:40 PM

Ever used MySql functions DATEDIFF() and TIMEDIFF()?

Ronald  ;D
RTFM is an almost extinct art form, it should be subsidized.

#3 GingerRobot

GingerRobot
  • Staff Alumni
  • Advanced Member
  • 4,086 posts
  • LocationUK

Posted 02 August 2006 - 01:44 PM

No, and i dont think those would quite do as i want because one of times in question is generated by the php script.

But ive just realised i dont need to use the function i made because i can use another mysql function, UNIX_TIMESTAMP, to convert the one from the database and just generate a unix timestamp in php.

Doh!

Thanks anyway :D




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users