acidglitter Posted January 24, 2008 Share Posted January 24, 2008 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? Quote Link to comment https://forums.phpfreaks.com/topic/87591-mysql-datetime/ Share on other sites More sharing options...
phpSensei Posted January 24, 2008 Share Posted January 24, 2008 Well try IF days is 0, then run a query for hours left, else just run a query that shows the days. Quote Link to comment https://forums.phpfreaks.com/topic/87591-mysql-datetime/#findComment-447998 Share on other sites More sharing options...
acidglitter Posted January 24, 2008 Author Share Posted January 24, 2008 Well try IF days is 0, then run a query for hours left, else just run a query that shows the days. i'm not sure how to code that in a mysql query though. could you write an example? Quote Link to comment https://forums.phpfreaks.com/topic/87591-mysql-datetime/#findComment-448004 Share on other sites More sharing options...
phpSensei Posted January 24, 2008 Share Posted January 24, 2008 It would be like... $query = mysql_query("SELECT.........etc DATEDIFF(DATE(thedate), CURDATE()) AS 'days_left'"); if(mysql_num_rows($query)==0){ // QUERY FOR HOURS LEFT } else { // QUERY FOR DAYS LEFT } Quote Link to comment https://forums.phpfreaks.com/topic/87591-mysql-datetime/#findComment-448020 Share on other sites More sharing options...
acidglitter Posted February 14, 2008 Author Share Posted February 14, 2008 how could i show how many hours are left? the query i'm using now (DATEDIFF(DATE(thedate), CURDATE()) AS 'days_left') only shows how many days are left Quote Link to comment https://forums.phpfreaks.com/topic/87591-mysql-datetime/#findComment-467128 Share on other sites More sharing options...
Barand Posted February 14, 2008 Share Posted February 14, 2008 SELECT IF(DATEDIFF(NOW(), logged) >= 1, DATEDIFF(NOW(), logged), TIMEDIFF(NOW(),logged)) as timeleft FROM tablename` Quote Link to comment https://forums.phpfreaks.com/topic/87591-mysql-datetime/#findComment-467255 Share on other sites More sharing options...
PFMaBiSmAd Posted February 14, 2008 Share Posted February 14, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/87591-mysql-datetime/#findComment-467260 Share on other sites More sharing options...
acidglitter Posted February 21, 2008 Author Share Posted February 21, 2008 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.. Quote Link to comment https://forums.phpfreaks.com/topic/87591-mysql-datetime/#findComment-473061 Share on other sites More sharing options...
acidglitter Posted February 22, 2008 Author Share Posted February 22, 2008 *bump* Quote Link to comment https://forums.phpfreaks.com/topic/87591-mysql-datetime/#findComment-473612 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.