Skip to main content

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



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

}




Comments

  1. 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.

    ReplyDelete
  2. You need to ensure that you are comparing the values in the same format. With what you currently have, on of three things will happen:


    If $due is an int, the string you are comparing it with 0000-00-00 00:00:00 will be converted to an int, which will result in 0, and $due will be compared with 0. This will only result in a match if $due === 0.
    If $due is a bool, the string will be converted to a bool, which will result in TRUE, and you will only get a match if $due === TRUE.
    If $due is any other type, it will be converted to a string and the two will be compared as strings. You will only get a match if $due === '0000-00-00 00:00:00'.


    If would say that you probably need to compare the values as integers. This means that you need to pass any strings through strtotime() - including $due, if it is a string. Since you do not show an example value of $due I cannot provide an exact working code.

    ReplyDelete

Post a Comment

Popular posts from this blog

Why is this Javascript much *slower* than its jQuery equivalent?

I have a HTML list of about 500 items and a "filter" box above it. I started by using jQuery to filter the list when I typed a letter (timing code added later): $('#filter').keyup( function() { var jqStart = (new Date).getTime(); var search = $(this).val().toLowerCase(); var $list = $('ul.ablist > li'); $list.each( function() { if ( $(this).text().toLowerCase().indexOf(search) === -1 ) $(this).hide(); else $(this).show(); } ); console.log('Time: ' + ((new Date).getTime() - jqStart)); } ); However, there was a couple of seconds delay after typing each letter (particularly the first letter). So I thought it may be slightly quicker if I used plain Javascript (I read recently that jQuery's each function is particularly slow). Here's my JS equivalent: document.getElementById('filter').addEventListener( 'keyup', function () { var jsStart = (new Date).getTime()...

Is it possible to have IF statement in an Echo statement in PHP

Thanks in advance. I did look at the other questions/answers that were similar and didn't find exactly what I was looking for. I'm trying to do this, am I on the right path? echo " <div id='tabs-".$match."'> <textarea id='".$match."' name='".$match."'>". if ($COLUMN_NAME === $match) { echo $FIELD_WITH_COLUMN_NAME; } else { } ."</textarea> <script type='text/javascript'> CKEDITOR.replace( '".$match."' ); </script> </div>"; I am getting the following error message in the browser: Parse error: syntax error, unexpected T_IF Please let me know if this is the right way to go about nesting an IF statement inside an echo. Thank you.