sfia Posted April 15, 2020 Share Posted April 15, 2020 Hi I'm trying to get this to work. I have known date in MySQL database entered as 2019-08-01 and I need to calculate from that date to current time. What I have is this code $date_ts = strtotime($row['date_ts']); //Timestamp $date_str = date("M-d-Y", $date_ts); //String in format MMM-DD-YYYY $TotalTime = floor((time() - $date_ts)/(60*60*24)) . ' days';//Total in days but my result showing as ==> 18367 days Could someone please help me get this to work so it shows the right number of days. thank you Quote Link to comment Share on other sites More sharing options...
requinix Posted April 15, 2020 Share Posted April 15, 2020 Either https://dev.mysql.com/doc/refman/8.0/en/date-and-time-functions.html#function_datediff or https://www.php.net/manual/en/class.datetime.php Quote Link to comment Share on other sites More sharing options...
Barand Posted April 15, 2020 Share Posted April 15, 2020 You could do in exactly the same way that I showed you in this earlier post (if you actually read it) 1 Quote Link to comment Share on other sites More sharing options...
gizmola Posted April 16, 2020 Share Posted April 16, 2020 I agree with Barand and requinix -- either use the functions in MySQL or use a DateTime. With that said, your code didn't work because you inexplicably added a line to turn a perfectly valid timestamp number back into a string, and then tried to subtract it from the timestamp number. If you hadn't done that your code would have worked! <?php $row['date_ts'] = '2019-08-01'; $date_ts = strtotime($row['date_ts']); //Timestamp $totalTime = floor((time() - $date_ts)/(60*60*24)) . ' days';//Total in days echo $totalTime; // Outputs 259 days Quote Link to comment 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.