ohno Posted May 12, 2021 Share Posted May 12, 2021 Hi, I have this code :- $datestarted = $row['datestarted']; $datestart=date("l d/m/Y @ H:i:s",strtotime($datestarted)); Which is been echo'd like this :- echo ' <tr align="center"'.$rowbackground.'> <td>'.$datestart.'</td> <td align="center"> '; So for example I get this output :-Sunday 18/04/2021 @ 10:45:26 The data in the DB for the above is stored like this :-2021-04-18 10:45:26 What I'd like to do is also echo how many days ago this date was, all of the examples I've tried don't seem to work though? Quote Link to comment https://forums.phpfreaks.com/topic/312679-calculate-number-of-days-ago-from-timestamp/ Share on other sites More sharing options...
Barand Posted May 12, 2021 Share Posted May 12, 2021 Take your pick, sql or php solution? $theDate = '2021-04-18 10:45:26'; $d1 = new DateTime($theDate); $elapsed = $d1->diff(new DateTime())->days; echo $elapsed; //--> 23 or SELECT datestarted , datediff(CURDATE(), datestarted) as elapsed FROM ... Quote Link to comment https://forums.phpfreaks.com/topic/312679-calculate-number-of-days-ago-from-timestamp/#findComment-1586501 Share on other sites More sharing options...
ohno Posted May 12, 2021 Author Share Posted May 12, 2021 Works a treat, thanks Quote Link to comment https://forums.phpfreaks.com/topic/312679-calculate-number-of-days-ago-from-timestamp/#findComment-1586505 Share on other sites More sharing options...
ohno Posted May 19, 2021 Author Share Posted May 19, 2021 On a similar note, can the date string be converted to minutes rather than days? (in PHP that is). I did try $elapsed = $d1->diff(new DateTime())->i ; Quote Link to comment https://forums.phpfreaks.com/topic/312679-calculate-number-of-days-ago-from-timestamp/#findComment-1586648 Share on other sites More sharing options...
ohno Posted May 19, 2021 Author Share Posted May 19, 2021 This works but doesn't add any hours outside of the whole days :- $elapsed = $d1->diff(new DateTime())->days * 24 * 60; eg, if the elapsed time is 7 days two hours it gives 10080 minutes instead of 10200. Quote Link to comment https://forums.phpfreaks.com/topic/312679-calculate-number-of-days-ago-from-timestamp/#findComment-1586650 Share on other sites More sharing options...
gizmola Posted May 19, 2021 Share Posted May 19, 2021 Take a look at the manual page for the Datetime::diff method. What you are returned is a "DateInterval" object. All you need to do is call the format method and utilize the appropriate format string -- it has already computed an internal representation of the interval between the 2 dates, and you don't want or need to do additional math to get this represented in days/hours/minutes etc. Just look at the format strings. Quote Link to comment https://forums.phpfreaks.com/topic/312679-calculate-number-of-days-ago-from-timestamp/#findComment-1586652 Share on other sites More sharing options...
ohno Posted May 19, 2021 Author Share Posted May 19, 2021 I'll take another look, none of the examples I seemed to find showed how this could be done though. Thanks Quote Link to comment https://forums.phpfreaks.com/topic/312679-calculate-number-of-days-ago-from-timestamp/#findComment-1586654 Share on other sites More sharing options...
Barand Posted May 19, 2021 Share Posted May 19, 2021 the properties of the DateInterval object that you will need are ->days ->h ->i so you will have some calculation but it's not rocket science. Note there is also a "d" property but that is the number of days as in "Difference = 5 months 3 days 6 hrs" Quote Link to comment https://forums.phpfreaks.com/topic/312679-calculate-number-of-days-ago-from-timestamp/#findComment-1586656 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.