Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Tell if PHP's DateTime is midnight

Tags:

php

datetime

Let's say I have the following dates/datetimes. How can I best tell which ones are at midnight (i.e. time is 00:00:00)? The first one should not be midnight, but the next three should be. Thanks

$d1=new DateTime('12/10/2012 05:33');
$d2=new DateTime('12/10/2012');
$d3=new DateTime('12/10/2012 00:00');
$d4=new DateTime('12/10/2012 00:00:00');
like image 872
user1032531 Avatar asked Jan 24 '13 17:01

user1032531


2 Answers

Check if the hour and minute are both zero:

if( $date->format( 'H') == 0 && $date->format( 'i') == 0) {
    echo "Midnight!";
}
like image 179
nickb Avatar answered Nov 05 '22 07:11

nickb


You will have to specify the time zone and use DateTime

$test = new DateTime('NOW', new DateTimeZone('Asia/Kolkata'));
$h=$test->format('H');
$m=$test->format('i');
$s=$test->format('s');
if( $h == 0 && $m==0 && $s == 0) {
    echo "Midnight!";
}
like image 1
Raj Nandan Sharma Avatar answered Nov 05 '22 07:11

Raj Nandan Sharma