Crew-Portal Posted November 8, 2007 Share Posted November 8, 2007 Okay all my times are set to $date = date('D M d, Y g:i a'); And this works fine posting data to MySQL but how do I get it so that it can take the time posted in the mysql which has the rowname `postdate` and subtract the time now so it can display something like: "POSTED PIREP 1365 Minutes Ago" You know? Any suggestions? Quote Link to comment Share on other sites More sharing options...
The Little Guy Posted November 8, 2007 Share Posted November 8, 2007 Give this a try (I haven't tried it though): <?php $time = date( "D M d, Y g:i a", strtotime(-now())); echo $time; ?> Quote Link to comment Share on other sites More sharing options...
Crew-Portal Posted November 8, 2007 Author Share Posted November 8, 2007 Doesnt seem to work! If someone could make it simpler by lets say I wanna subtract 2 hours 5 mins and 18 seconds from the current time, if someone could write the code showing how to do that it would be a life saver, then I could figure out myself on how to imply it to mySQL I looked on PHP.net but I couldnt understand how to do it, each time I tried I got a result that I didnt want. I know im asking for a bit but if someone could just lend me like 3 mins and write to code... -2 hours 5 mins and 18 seconds Quote Link to comment Share on other sites More sharing options...
mattcairns Posted November 8, 2007 Share Posted November 8, 2007 you can use strtotime(); $posted_date = "2007-11-07 23:8:00"; $posted_date = strtotime($posted_date); $cur_date = strtotime("now"); $time_pass = $cur_date - $posted_date; $elapsed_time = date("i",$time_pass); print "$elapsed_time minutes since last post."; Quote Link to comment Share on other sites More sharing options...
Crew-Portal Posted November 8, 2007 Author Share Posted November 8, 2007 I understand strtotime but I cant figure out how to use it. Can you read the post above yours that would help alot! Quote Link to comment Share on other sites More sharing options...
mattcairns Posted November 8, 2007 Share Posted November 8, 2007 from what i've found, when doing date and time math it is easiest to convert the time to the unix time stamp. that way you are dealing with one unit(seconds) and not multiple units like days, minutes, seconds, etc. so if you need to tell the difference between 2 dates/times, you could convert them both to their unix equivalents and do the math from there. example: $time1 = strtotime($time1); $time2 = strtotime($time2); $diff = $time2- $time1; $output = date("h:i:s", $diff); Now if you want to subtract from the current time all you have to do is: strtotime("-2 hours 5 minutes 18 seconds"); I doubt that is what you really want to do since you have a time stored in a database though. I think you will find the first option to be what you want with a little work. Quote Link to comment Share on other sites More sharing options...
Crew-Portal Posted November 8, 2007 Author Share Posted November 8, 2007 Thank You! Beautiful it works like a dream come true! TOPIC SOLVED!!! (Oh God Ive written that like 43 times!) Quote Link to comment Share on other sites More sharing options...
mattcairns Posted November 8, 2007 Share Posted November 8, 2007 glad to help. 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.