32

How can I get the timestamp of 12 o'clock of today, yesterday and the day before yesterday by using strtotime() function in php?

12 o'clock is a variable and would be changed by user.

Martin Smith
  • 438,706
  • 87
  • 741
  • 845
hd.
  • 17,596
  • 46
  • 115
  • 165
  • Depends on how you store the 12 o'clock (hours, minutes, seconds?). In general, did you have a look at the functions listed in (http://php.net/manual/en/book.datetime.php)? What did you try so far? – Martin Hennings Jan 24 '11 at 09:24
  • 1
    definite duplicate of [getting timestamp in php](http://stackoverflow.com/questions/4772953/getting-timestamp-in-php) - You already asked that yesterday. The only difference is the arguments you pass to these functions. You can find the possible relative formats in the [PHP Manual](http://de2.php.net/manual/en/datetime.formats.relative.php) – Gordon Jan 24 '11 at 09:26
  • @Gordon: i am sorry.i am really stupid today.i wanted to delete it but it has some answer now and i can't delete it. – hd. Jan 24 '11 at 09:40

9 Answers9

69
$hour = 12;

$today              = strtotime($hour . ':00:00');
$yesterday          = strtotime('-1 day', $today);
$dayBeforeYesterday = strtotime('-1 day', $yesterday);
472084
  • 17,666
  • 10
  • 63
  • 81
deceze
  • 510,633
  • 85
  • 743
  • 889
  • 4
    +1 and i know its nitpicking but I would prefer `$today = strtotime($hour . ":00:00");` – Hannes Jan 24 '11 at 09:24
  • 12
    @Hannes Then I'd prefer `$hour . ':00:00'`. If you don't need variable interpolation, use single quotes. :o) – deceze Jan 24 '11 at 09:26
  • strtotime(): It is not safe to rely on the system's timezone settings. You are *required* to use the date.timezone setting or the date_default_timezone_set() function – Pavel Hasala Jul 04 '17 at 22:45
  • 1
    `strtotime('yesterday')` seems to work on my system. – Altimus Prime Feb 27 '19 at 21:10
17

strtotime supports a number of interesting modifiers that can be used:

$hour = 12;

$today              = strtotime("today $hour:00");
$yesterday          = strtotime("yesterday $hour:00");
$dayBeforeYesterday = strtotime("yesterday -1 day $hour:00");

echo date("Y-m-d H:i:s\n", $today);
echo date("Y-m-d H:i:s\n", $yesterday);
echo date("Y-m-d H:i:s\n", $dayBeforeYesterday);

It works as predicted:

2011-01-24 12:00:00
2011-01-23 12:00:00
2011-01-22 12:00:00
hakre
  • 193,403
  • 52
  • 435
  • 836
Emil H
  • 39,840
  • 10
  • 78
  • 97
14

OO Equivalent

$iHour = 12;

$oToday = new DateTime();
$oToday->setTime($iHour, 0);

$oYesterday = clone $oToday;
$oYesterday->modify('-1 day');

$oDayBefore = clone $oYesterday;
$oDayBefore->modify('-1 day');

$iToday     = $oToday->getTimestamp();
$iYesterday = $oYesterday->getTimestamp();
$iDayBefore = $oDayBefore->getTimestamp();

echo "Today: $iToday\n";
echo "Yesterday: $iYesterday\n";
echo "Day Before: $iDayBefore\n";
enobrev
  • 22,314
  • 7
  • 42
  • 53
4

You can easily find out any date using DateTime object, It is so flexible

$yesterday = new DateTime('yesterday');
echo $yesterday->format('Y-m-d');

$firstModayOfApril = new DateTime('first monday of april');
echo $firstModayOfApril->format('Y-m-d');

$nextMonday = new DateTime('next monday');
echo $nextMonday->format('Y-m-d');
Nisam
  • 2,275
  • 21
  • 32
3

to get start of day yesterday

$oDate = new DateTime(); 
$oDate->modify('-1 day'); 
echo $oDate->format('Y-m-d 00:00:00');

result

2014-11-05 00:00:00
zzapper
  • 4,743
  • 5
  • 48
  • 45
1

All the answers here are too long and bloated, everyone loves one-lines ;)

$yesterday = Date('Y-m-d', strtotime('-1 day'));

(Or if you are American you can randomize the date unit order to m/d/y (or whatever you use) and use Cups, galloons, feet and horses as units...)

OZZIE
  • 6,609
  • 7
  • 55
  • 59
0

As of PHP 7 you can write something like this:

$today = new \DateTime();
$yesterday = (clone $today)->modify('-1 day');
$dayBefore = (clone $yesterday)->modify('-1 day');
// Then call ->format('Y-m-d 00:00:00'); on each objects
Nicholas Shanks
  • 10,623
  • 4
  • 56
  • 80
COil
  • 7,201
  • 2
  • 50
  • 98
0

you can also use new DateTime("now") for today new DateTime("1 day ago") for yesterday or all can be parse by strtotime php function.

Then format as you want.

Jean-Luc Barat
  • 1,147
  • 10
  • 18
0
$timeStamp = time();
// $timeStamp = time() - 86400;

if (date('d.m.Y', $timeStamp) == date('d.m.Y')) {    
  echo 'Today'; 
} elseif (date('d.m.Y', $time) == date('d.m.Y', strtotime('-1 day'))) {     
  echo 'Yesterday';
}