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
https://forums.phpfreaks.com/topic/87591-mysql-datetime/
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
https://forums.phpfreaks.com/topic/87591-mysql-datetime/#findComment-467260
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
https://forums.phpfreaks.com/topic/87591-mysql-datetime/#findComment-473061
Share on other sites

Archived

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.