Jump to content

Subtract date and time from another date and time, display difference.


Recommended Posts

Hey guys,

How would I go about subtracting Today from a previous day to find the difference?


For example, I want to subtract TODAY from a previous date in my database, to determine if the difference is greater than 1 day.


Any ideas? I tried doing the subraction in TIMESTAMPS, but when I convert the date back to Y-m-d H:i:s, I got some weird year and time.



Link to comment
Share on other sites

// Create DateTime object with your timestamp
$date = DateTime::createFromFormat("Y-m-d H:i:s", '2010-02-15 15:16:17');
// Calculate the difference between your timestamp and "now"
$interval = $date->diff(new DateTime());
// Get the data stored in the DateInterval
echo $interval->format("Differernce is %d days, %h hours %i minutes and %s seconds.");

Link to comment
Share on other sites

Ok..so i'm stuck.


How would I use mysql's DATEDIFF to subtract a date in the database called 'expire' by the current date and time?


This doesn't work:


$result = mysql_query ("SELECT * FROM sample WHERE username = '$username' AND DATEDIFF('$today', 'expire'");



Link to comment
Share on other sites


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

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