Jump to content

mysql datetime


acidglitter

Recommended Posts

This is what I have right now to get the number of days left from the date saved until now..

 

DATEDIFF(DATE(thedate), CURDATE()) AS 'days_left'

 

What I want to do is keep it the way it is, BUT if there is only one day left, show how many hours or minutes left. I'm not really sure how to do that. I'm thinking a subquery might work so even if theres a few days left or hours it will still be called "days_left".... Could anyone help me with this?

Link to comment
Share on other sites

  • 3 weeks later...

Edit: Similar to Barand's code above ^^

 

No need for any subquery or slow php code, just a mysql IF() statement (assuming that 'thedate' is a DATETIME data type -

 

$query = "SELECT IF(DATEDIFF(DATE(thedate), CURDATE()) > 0,
DATEDIFF(DATE(thedate), CURDATE()),
SUBTIME(TIME(thedate), CURTIME())) AS 'days_left' FROM your_table";

 

The above works when thedate is greater than the current date/time, otherwise you get negative numbers.

Link to comment
Share on other sites

thanks for the replies!

this is what i have now then...

SELECT *,
IF(DATEDIFF(DATE(thedate), CURDATE()) > 1,
DATEDIFF(DATE(thedate), CURDATE()),
SUBTIME(TIME(thedate), CURTIME())) AS 'days_left'

 

and it works like you said, if there are 2 days or more its a whole number.. if its less than a day its a negative number.. but i want it to be a positive number for how many hours left instead and i'm not sure what to change it so it does that..

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.