Jump to content

[SOLVED] Date posted


aran

Recommended Posts

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?

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.