Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if $date is equal to 0000-00-00 00:00:00

Tags:

date

php

I want a logic if $due is equal to no date then I want data starting with 2000. My code below is not working.

if($due == '0000-00-00 00:00:00'){
    $due =  strtotime('2000-00-00 00:00:00');
}
like image 891
Muhammad Imran Tariq Avatar asked Jan 13 '12 16:01

Muhammad Imran Tariq


2 Answers

If you are storing strtotime() in $due, then you need to compare it like that, too

if($due == strtotime('0000-00-00 00:00:00')){
    $due =  strtotime('2000-00-00 00:00:00');
}

or, more simply

if($due == 0){
    $due =  strtotime('2000-00-00 00:00:00');
}

but if $due is coming from your db as a string, you would want to strtotime() it:

$due = strtotime($due);
if($due == 0){
    $due =  strtotime('2000-00-00 00:00:00');
}

Be aware of timezones, though. That is, strtotime('0000-00-00 00:00:00 UTC') != strtotime('0000-00-00 00:00:00 EST'). This could also break your compare.

like image 129
Umbrella Avatar answered Sep 21 '22 12:09

Umbrella


I don't know why strtotime didnt work correctly for me. Probably it's because of time zone and other stuff.

As always fancy regular expressions works like a charm. I'm somehow addicted to it!

if(preg_match("/0{4}/" , $dbDateField))
{
    // no its not acceptable
}
else
{
    //ok
}
like image 42
Hossein Khalesi Avatar answered Sep 20 '22 12:09

Hossein Khalesi