开发者

Little help with this "date" function

I have this to select a date from a mysql db, and compare it to an array of month-names in swedish language.

$monthnames = array("","Januari","Februari开发者_如何学JAVA","Mars","April","Maj","Juni","Juli","Augusti","September","Oktober","November","December");

$postdate = $monthnames[date("n", strtotime( $row['modify_date'] ))];
//Outputs something like '12 Februari'

Here is the prob, I want to check the $postdate variable and change it to "Today", "Yesterday" and "Day before yesterday" according to the date, how can I do so?

Thanks


If you're using timestamps to store dates in the database:

You can have preset intervals like:

$day = mktime(0,0,0,2,1,2001)-mktime(0,0,0,1,1,2001);

And you can compare the time now - timestamps/dates you have in the database with those intervals.

For example, let's say $dbTime contains a timestamp fetched with mysql:

$time = time() - $dbTime;
if($time<$day)
   echo 'posted today';
elseif($time<($day*2))
   echo 'posted yesterday';

etc etc.

Then you can use PHP's Date function to echo hrs, minutes, secs, or dates, if they are larger than your predefined intervals.


There is no such in-built function in PHP. You got to do calculations, the mktime function can be your friend in this case.

if (date of $timenow == date of $timepost) { today; }
else if (date of $timenow - 24h == date of $timepost) { yesterday; }
etc.

Be aware about summer/winter time change, timezones etc. so try to use mktime()

http://php.net/date

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜