4

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 delta to get the date before yesterday and converting it to a particular format. Messy.

Fokko Driesprong
  • 2,075
  • 19
  • 31
Mridang Agarwalla
  • 43,201
  • 71
  • 221
  • 382

2 Answers2

5

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.

Fokko Driesprong
  • 2,075
  • 19
  • 31
  • nice suggestion thinking outside the box! is there a chance this would ever round _up_ to the _next day_?? – Yes Barry Aug 23 '13 at 20:03
2

you can use the strtotime function

<?php

$time = strtotime("5 june 2010");
$before = strtotime("-1 day",$time);
$after = strtotime("+1 day",$time);
Spidfire
  • 5,433
  • 6
  • 28
  • 36