Jump to content


Photo

Converting And Subtracting Dates


  • Please log in to reply
3 replies to this topic

#1 JustinK101

JustinK101
  • Members
  • PipPipPip
  • Advanced Member
  • 503 posts
  • LocationSan Diego, California, US

Posted 02 October 2006 - 11:38 PM

Hello, I need to preform a date/time conversion.

I have the following data:

$date which stores a date in mysql format, yyyy-mm-dd
$time which stores the time in mysql format hh:mm:ss

Then I need to grab the current date and time from the server and subtract the difference from the current date/time against my custom $date/$time and return the result in the form of hours. So an hour and half from the current date/time to the stored date/time would be returned as 1.5, etc. Thanks much!



#2 JustinK101

JustinK101
  • Members
  • PipPipPip
  • Advanced Member
  • 503 posts
  • LocationSan Diego, California, US

Posted 03 October 2006 - 12:05 AM

here is my solution, I think this works, any better way of doing it though?

$my_date = $row[0];
$my_time = $row[1];

$my_epoch = strtotime($my_date . "T" . $my_time);
$this_epoch = time();

$total = ((($this_epoch - $my_epoch) / 60) / 60);
$total = round($total, 2);

#3 ponsho

ponsho
  • Members
  • PipPip
  • Member
  • 24 posts

Posted 03 October 2006 - 12:20 AM

list($year,$month,$day) = explode("-",$date);
list($hour,$min,$sec)  = explode(":",$time);
$time  = mktime(date("h"),date("i"),date("s"),date("m"),date("d"),date("Y"));
$time2  = mktime($hour,$min,$sec,$month,$day,$year);
$total  = round(($time-$time2)/3600,2);

#4 JustinK101

JustinK101
  • Members
  • PipPipPip
  • Advanced Member
  • 503 posts
  • LocationSan Diego, California, US

Posted 03 October 2006 - 03:46 AM

ponsho:

Interesting, thanks, your code is nice, though I think a bit harder to read, probably just my personal preference though.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users