Jump to content

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


stangn99

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.

:confused:

 

Link to comment
Share on other sites

<?php
// 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 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.