Php: How to Check If a Date Is Today, Yesterday or Tomorrow

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 check if my date from db is today echo with today and if tomorrow then echo tomorrow

<?php

$date='14-Nov-2017';
$time = strtotime($date);
$formatDate = date('Y-m-d',$time);
echo $formatDate;
$today=date("Y-m-d");

if($formatDate==date("Y-m-d")){
echo "TODAY";
}
else if ($formatDate==date('Y-m-d', strtotime($today. ' +1 days'))){
echo 'TOMORROW';
}
else{
echo "Another day";
}

This code get's the date you output , and compares it with todays date , tomorrows date otherwise just echos another date. Tested and it works also.

How to check if date is today's date with PHP

UPDATE: As MySQL query, you can do this as follows:

$db->query("DELETE * FROM entry WHERE DATE(date) = CURDATE()");

CURDATE() returns today's date.

If you want to do with php, see below to use the DateTime class.

$now = new DateTime;
$otherDate = new DateTime('2016-01-01'); // or e.g. 2016-01-01 21:00:02

// Setting the time to 0 will ensure the difference is measured only in days
$now->setTime( 0, 0, 0 );
$otherDate->setTime( 0, 0, 0 );

var_dump($now->diff($otherDate)->days === 0); // Today
var_dump($now->diff($otherDate)->days === -1); // Yesterday
var_dump($now->diff($otherDate)->days === 1); // Tomorrow

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

How to get birthday date in form of today, tomorrow, yesterday?

Maybe a bit complicated solution, but here I compare month num and date num:

$current_month = date("n");
$current_day = date("j");

// date of birth
$dob = strtotime("1991-07-26");
$dob_month = date("n", $dob);
$dob_day = date("j", $dob);

if ($current_month == $dob_month) {
if ($current_day == $dob_day) {
echo 'TODAY';
} elseif ($current_day == $dob_day + 1) {
echo 'YESTERDAY';
} elseif($current_day == $dob_day - 1) {
echo 'TOMORROW';
} else {
echo 'IN this month';
}
} elseif ($current_month < $dob_month) {
echo 'In future';
} else {
echo 'Long back';
}

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

To check weather the date selected by date picker is yesterday

You can build DateTime objects and compare them.

<?php
$checkin = new DateTime($_POST['checkin']);
$checkout = new DateTime($_POST['checkout']);
$today = new DateTime();

if ($checkin >= $today && $checkout > $checkin) {
// proceed to order
}

PHP date time greater than today

You are not comparing dates. You are comparing strings. In the world of string comparisons, 09/17/2015 > 01/02/2016 because 09 > 01. You need to either put your date in a comparable string format or compare DateTime objects which are comparable.

<?php
$date_now = date("Y-m-d"); // this format is string comparable

if ($date_now > '2016-01-02') {
echo 'greater than';
}else{
echo 'Less than';
}

Demo

Or

<?php
$date_now = new DateTime();
$date2 = new DateTime("01/02/2016");

if ($date_now > $date2) {
echo 'greater than';
}else{
echo 'Less than';
}

Demo



Related Topics



Leave a reply



Submit