开发者

Compare timestamp to date

I need to compare a timestamp to a date. I would just like to compare the date portion without the time bit. I need to check whether a timestamp occurs on the day before yesterday i.e. today - 2.

Could you show me a snippet please? Thank you.

I've been reading through the PHP docs but couldn't find a very clean way of doing this. What I found was converting the timestamp to a date with a particular format and comparing it to a date which I get by doing a time de开发者_运维问答lta to get the date before yesterday and converting it to a particular format. Messy.


You can arcieve this by using the function strtotime.

To round to a day I personaly like to edit the timestamp. This is a notations of seconds since epoch. One day is 86400 seconds, so if you do the following caculation:

$time = $time - ( $time  % 86400 );

You can convert it back to a date again with the date function of PHP, for example:

$readableFormat = date( 'd-m-Y', $time );

There is also much on the internet about this topic.


you can use the strtotime function

<?php

$time = strtotime("5 june 2010");
$before = strtotime("-1 day",$time);
$after = strtotime("+1 day",$time);
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜