Home » Php » datetime – Is there a PHP function to test a zero date

datetime – Is there a PHP function to test a zero date

Posted by: admin April 23, 2020 Leave a comment


Looking for a shorthand or better way to test a zero date (MySQL datetime column) other than:

if ($row['this_date'] == '0000-00-00 00:00:00') echo 'No date set.';

Is there a PHP function that test it? Thanks.

How to&Answers:

I think you don’t need any shorthand.
It’s sort of some micro-optimization.

You have already spent more time asking this question than can save you such a shorthand in a year of average coding. Just write it and move on.
Don’t you have more important problems to solve?

Moreover, with such a shorthand you will obfuscate your own code.
Your current statement is perfectly clear, reads "if date is empty"
but all proposed snippets aren’t that clear. Coming to this code months later, you will puzzle yourself with question, if such a code monster have any special meaning.

What you actually wanted is readability. But you have it already. While all proposed shorthands don’t.


What about:

if(strtotime($row['this_date']) == 0) echo 'No date set.';


Although you should probably use NULL in your database, a simple hack would be:

if(!(int)$row['this_date'])echo'No Date Set.';

What kind of better way are you expecting to find? it’s fast as it is.

P.S. It’s a hack because I’m assuming you will have a year set. Meaning it won’t work for the string 0000-00-00 00:00:01


if (strtotime($row['this_date'])) echo 'No date set.';


Being a fan of regular expressions, I’ll add another one:

if (!preg_match('/[1-9]/', $row['this_date'])) echo 'No date set.';
  • Works also with timestamps and other stuff.
  • Obviously does not check for date consistency.


You can use the following code

$date = '0000-00-00 00:00:00';
if(str_replace(array("-", " ", ":"), "", $date)*1>0){
    echo "DATE";
} else {
    echo "NODATE";


I’m using:

if ($timedate->format('U') == -62169962400){
   // 0000-00-00

format('U') will return Seconds since the Unix Epoch (January 1 1970 00:00:00 GMT) The number may need to be adjusted for the timezone


Use checkdate

var_dump(checkdate(12, 31, 2000));
var_dump(checkdate(2, 29, 2001));

The above example will output:




This is a function I use for the exact same thing:

 * Like empty(), but also returns true for empty MySQL DATE ('0000-00-00') and DATETIME ('0000-00-00 00:00:00')
 * @param mixed $var Variable to check
 * @return bool
function empty_date($var)
    if (empty($var) or $var === '0000-00-00' or $var === '0000-00-00 00:00:00')
        return true;
        return false;


This solution works for me.

'0000-00-00' value is stored in my db table.

function checkValidDate($date){
   $timestamp = strtotime($date);
   return (!empty($timestamp) && $date!='0000-00-00') ? date('d-M-Y',$timestamp) : null;


It should be avoided at all costs to use and store zero dates. This function could help close the gap until they are fully removed from your code. May be it could be called “isEmptyDate” or “isNullDate”.

// Returns 'true' for zero, zero dates or times, null, false
function isZeroDate($date)
  if (empty($date)) return true;

  if (is_string($date)) {
    // Extract any character that it's not digit and convert it to int
    // 0000-00-00 00:00 || 0000 || 00:00 || 0000-00-00 will return true
    $ival = intval(preg_replace('~\D~', '', $date), 10);
    if ($ival == 0) return true;

  return false;

Carbon and DateTime classes cannot parse zero dates with this format “0000-00-00 00:00:00”.

With any PHP framework like Laravel, use nullable columns in your migrations to store NULL dates in DB

eg. in Laravel:


eg in MySql:

create table date_test (some_date datetime null default null);
insert into date_test (some_date) values (null);


Something I currently use and was exploring to hopefully find a better solution…

function prep_empty_fields(&$data) {
$is_obj = is_object($data);
$is_arr = is_array($data);
if ($is_obj || $is_arr) {
    $empty_values = array(
        '0000', //year
        '0000-00-00 00:00:00', //datetime
        '0000-00-00', //date
        '00:00:00', //time
    foreach ($data as $key => $value) {
        if (in_array($value, $empty_values)) {
            if ($is_obj) {
                $data->$key = '';
            } else {
                $data[$key] = '';
        } elseif (strpos($value, '<') !== FALSE) { //has html characters
            $value = htmlentities($value);
            if ($is_obj) {
                $data->$key = $value;
            } else {
                $data[$key] = $value;

My code simply gets rid of the “empty” values, but may be a good starting point for you.