aran Posted January 3, 2008 Share Posted January 3, 2008 I am currently having a few problems trying to figure out a good way to count how long ago a post was made... Basically I have a article PHP script which I got of a tutorial site, and I am now trying to add the ability to count how old that post is. The script saves the date and time in the MySQL database of when it was posted. I have found various scripts on formatting that date and I have been successfully in doing this. Is there anyone that could help me out in making my code count how old a post is? If the date was formatted like 03/01/08 or 03/01/2008 how would I check how long ago that date is? Quote Link to comment https://forums.phpfreaks.com/topic/84330-solved-date-posted/ Share on other sites More sharing options...
revraz Posted January 3, 2008 Share Posted January 3, 2008 Don't use that date format, use either Unix timestamp or MySQL timestamp. If the date was formatted like 03/01/08 or 03/01/2008 how would I check how long ago that date is? Quote Link to comment https://forums.phpfreaks.com/topic/84330-solved-date-posted/#findComment-429491 Share on other sites More sharing options...
Zane Posted January 3, 2008 Share Posted January 3, 2008 the easiest way is to use strtotime take your source date 03/01/08 or 03/01/2008 put it in the function strtotime("03/01/08")...which yeilds 1204952400 get the current days date 01/03/08 or 01/03/08 strtotime("01/03/08")..yeilds 1199336400 find the difference between 1199336400 and 1204952400 = 5616000 UNIX time is all in seconds so that's 5616000 seconds 5616000 seconds => 93600 minutes 93600 minutes => 1560 hours 1560 hours => 65 days 65 days which nears around 2 months or so...give or take. and what do you know...2 months or so (considering leapyear) from today it will be March 3rd Quote Link to comment https://forums.phpfreaks.com/topic/84330-solved-date-posted/#findComment-429526 Share on other sites More sharing options...
aran Posted January 3, 2008 Author Share Posted January 3, 2008 thing is am formatting stuff in UK time format... so it should of actually been 2 days ago... How would I go about finding the difference just take it away then do certain calculations ? Quote Link to comment https://forums.phpfreaks.com/topic/84330-solved-date-posted/#findComment-429559 Share on other sites More sharing options...
aran Posted January 4, 2008 Author Share Posted January 4, 2008 I successfully been able to do what I wanted to do but I have now hit a problem in a if statement trying to match that date difference. Basically for stuff that was posted on the same day it does not work. the code am using is: if($days == 0) { $todaysposts++; } and this is not working and I cant figure out why... $days is actually outputting 0 but the if statement cant identify it... Any thoughts? Quote Link to comment https://forums.phpfreaks.com/topic/84330-solved-date-posted/#findComment-429909 Share on other sites More sharing options...
redarrow Posted January 4, 2008 Share Posted January 4, 2008 $days=$_POST['days']; if($days == 0) { $todaysposts++; } Quote Link to comment https://forums.phpfreaks.com/topic/84330-solved-date-posted/#findComment-430074 Share on other sites More sharing options...
neilfurry Posted January 4, 2008 Share Posted January 4, 2008 Basically you can make use of strtotime function like: <? $tm ="01/04/2008"; $t = strtotime('Y-m-d',$tm); $num_of_days=date('Y-m-d')-$t; echo $num_of_days; ?> Quote Link to comment https://forums.phpfreaks.com/topic/84330-solved-date-posted/#findComment-430350 Share on other sites More sharing options...
Yesideez Posted January 4, 2008 Share Posted January 4, 2008 What I do whenever I store a date and/or time value is to store time() in a database as an unsigned integer which happens to save both date and time. When showing the value in human form I show it using date() If I want to compare two dates & times all I do is compare one against the other, eg. if ($date1<$date2) {echo 'date1 lower than date2';} if ($date1==$date2) {echo 'date1 same as date2';} if (date("h:i:S",$date1)==date("h:i:S",$date2)) {echo 'Times in both dates are the same';} Hope that helps. Quote Link to comment https://forums.phpfreaks.com/topic/84330-solved-date-posted/#findComment-430358 Share on other sites More sharing options...
Yesideez Posted January 4, 2008 Share Posted January 4, 2008 thing is am formatting stuff in UK time format... so it should of actually been 2 days ago... How would I go about finding the difference just take it away then do certain calculations ? PHP has a handy function gmdate() which converts the time on the server to the GMT equivalent but as to how reliable this is, what it relies on and if it can be trusted completely to return the correct time I'm not sure. Quote Link to comment https://forums.phpfreaks.com/topic/84330-solved-date-posted/#findComment-430361 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.