Mridang Agarwalla
Mridang Agarwalla

Reputation: 44958

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

Upvotes: 4

Views: 4120

Answers (2)

Fokko Driesprong
Fokko Driesprong

Reputation: 2250

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.

Upvotes: 5

Spidfire
Spidfire

Reputation: 5523

you can use the strtotime function

<?php

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

Upvotes: 2

Related Questions