33

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.

3
  • Depends on how you store the 12 o'clock (hours, minutes, seconds?). In general, did you have a look at the functions listed in (php.net/manual/en/book.datetime.php)? What did you try so far? Commented Jan 24, 2011 at 9:24
  • 1
    definite duplicate of 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 Commented Jan 24, 2011 at 9: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. Commented Jan 24, 2011 at 9:40

10 Answers 10

69
$hour = 12; $today = strtotime($hour . ':00:00'); $yesterday = strtotime('-1 day', $today); $dayBeforeYesterday = strtotime('-1 day', $yesterday); 
Sign up to request clarification or add additional context in comments.

4 Comments

+1 and i know its nitpicking but I would prefer $today = strtotime($hour . ":00:00");
@Hannes Then I'd prefer $hour . ':00:00'. If you don't need variable interpolation, use single quotes. :o)
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
strtotime('yesterday') seems to work on my system.
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 

Comments

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"; 

Comments

4

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 

Comments

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'); 

1 Comment

$days = 7; (new DateTime("now -{$days} days"))->format('c');
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...)

Comments

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 

Comments

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.

Comments

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'; } 

Comments

0

Get timestamp of today

$iTimestamp = new \DateTime()->getTimestamp(); 

Get timestamp of yesterday

$iTimestamp = new \DateTime("1 day ago")->getTimestamp(); 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.