Jump to content

Converting And Subtracting Dates


JustinK101

Recommended Posts

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!

Link to comment
Share on other sites

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

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.