Get Timestamp of Today and Yesterday in PHP

Get timestamp of today and yesterday in php

$hour = 12;

$today = strtotime($hour . ':00:00');
$yesterday = strtotime('-1 day', $today);
$dayBeforeYesterday = strtotime('-1 day', $yesterday);

how to get date of yesterday using php?

there you go

date('d.m.Y',strtotime("-1 days"));

this will work also if month change

get records of today, yesterday and this week

Make your calculations based on CURDATE if you are selecting date only.

Hope the below Examples would help you

Today: WHERE timestamp >= CURDATE()
Yesterday: WHERE timestamp >= DATE_SUB(CURDATE(), INTERVAL 1 DAY) AND timestamp < CURDATE()
This month: WHERE timestamp >= DATE_SUB(CURDATE(), INTERVAL DAYOFMONTH(CURDATE())-1 DAY)
Between the two dates 3 June 2013 and 7 June 2013 (note how the end date is specified as 8 June, not 7 June): WHERE timestamp >= '2013-06-03' AND timestamp < '2013-06-08'

Please see this one too..

PHP: date Yesterday, Today

function get_day_name($timestamp) {

$date = date('d/m/Y', $timestamp);

if($date == date('d/m/Y')) {
$date = 'Today';
}
else if($date == date('d/m/Y',now() - (24 * 60 * 60))) {
$date = 'Yesterday';
}
return $date;
}
print date('G:i:s', $last_access).' '.get_day_name($last_access);

PHP: How to check if a date is today, yesterday or tomorrow

First. You have mistake in using function strtotime see PHP documentation

int strtotime ( string $time [, int $now = time() ] )

You need modify your code to pass integer timestamp into this function.

Second. You use format d.m.Y H:i that includes time part. If you wish to compare only dates, you must remove time part, e.g. `$date = date("d.m.Y");``

Third. I am not sure if it works in the same way for you, but my PHP doesn't understand date format from $timestamp and returns 01.01.1970 02:00 into $match_date

$timestamp = "2014.09.02T13:34";
date('d.m.Y H:i', strtotime($timestamp)) === "01.01.1970 02:00";

You need to check if strtotime($timestamp) returns correct date string. If no, you need to specify format which is used in $timestamp variable. You can do this using one of functions date_parse_from_format or DateTime::createFromFormat

This is a work example:

$timestamp = "2014.09.02T13:34";

$today = new DateTime("today"); // This object represents current date/time with time set to midnight

$match_date = DateTime::createFromFormat( "Y.m.d\\TH:i", $timestamp );
$match_date->setTime( 0, 0, 0 ); // set time part to midnight, in order to prevent partial comparison

$diff = $today->diff( $match_date );
$diffDays = (integer)$diff->format( "%R%a" ); // Extract days count in interval

switch( $diffDays ) {
case 0:
echo "//Today";
break;
case -1:
echo "//Yesterday";
break;
case +1:
echo "//Tomorrow";
break;
default:
echo "//Sometime";
}

Php get timestamp of previous day midnight

try this

$date = new DateTime();
$date->setTimestamp(strtotime('yesterday midnight'));

Get yesterday time PHP

In your example 0:00:00 is the very first minute of TODAY. This is why it does not work.

You can use strtotime to fetch yesterday and then format the date as you want:

$from = date("Y-m-d 00:00:00", strtotime("yesterday"));

PHP date yesterday

date() itself is only for formatting, but it accepts a second parameter.

date("F j, Y", time() - 60 * 60 * 24);

To keep it simple I just subtract 24 hours from the unix timestamp.

A modern oop-approach is using DateTime

$date = new DateTime();
$date->sub(new DateInterval('P1D'));
echo $date->format('F j, Y') . "\n";

Or in your case (more readable/obvious)

$date = new DateTime();
$date->add(DateInterval::createFromDateString('yesterday'));
echo $date->format('F j, Y') . "\n";

(Because DateInterval is negative here, we must add() it here)

See also: DateTime::sub() and DateInterval

Find past week, 2 days ago, yesterday and today with a PHP date

I would not convert the dates to their textual representations. Instead I would use timestamps and an array to match timestamps to texts.

Something like:

$times = array(
86400 => 'yesterday',
172800 => 'two days ago'
// etc
);

$time = strtotime($entrydate);

if ( $time < (time() - 172800) ) {
echo $times[172800];
} elseif ( $time < (time() - 86400) ) {
echo $times[86400];
}
// etc.

You could probably do this in a loop or more intelligently to avoid all the repetition, but this is the general idea.

get start time of yesterday in unix time stamp(php)

This will get you yesterday's timestemp

For yesterday date

echo "Yesterday date".time() - 86400; //this will take current time

For last day from given day

$inputedDate = "2016:12:25 00:00:00"; // you can pass date you want
$yesterdayDate = date ("Y-m-d H:i:s",strtotime($inputedDate)-86400);
echo $yesterdayDate;


Related Topics



Leave a reply



Submit