0

I' am comparing date time with today's date time to show or not show the content if its past date. I have checkout this Question on Stackoverflow but with no success.

This is the Code

function vacancy_expire($id, $close_date){
        date_default_timezone_set("Pacific/Fiji");

        $today = "2014-04-30 04:04";
        $close_date = "2014-04-30 16:00";
        if( $today > $close_date ){
           echo "1";
        } else {
           echo "0";
        }
}
Community
  • 1
  • 1
Navneil Naicker
  • 3,586
  • 2
  • 21
  • 31

3 Answers3

1

If you change the above as follows

function vacancy_expire($id, $close_date) {
    date_default_timezone_set("Pacific/Fiji");

    $today = strtotime("2014-04-30 04:04");
    $close_date = strtotime("2014-04-30 16:00");
    if( $today > $close_date ){
       echo "1";
    } else {
       echo "0";
    }
}

I hope this will be helpful.

Kshitiz
  • 2,673
  • 1
  • 18
  • 24
  • I'm not understanding this proposed solution to the reported problem. The function is being passed an argument of `$close_date`, but the parameter value is being ignored. The variables are being set to constant values. The entire body of the function could be replaced with one line: "`echo "0";`". – spencer7593 Apr 30 '14 at 04:30
1

You are missing the strtotime function here.

$today & $close_date in your function is a string which needs to be converted to date&time.

follow this:

<?php

function vacancy_expire($id, $close_date){
    date_default_timezone_set("Pacific/Fiji");

    $today = time();

    $close_date = strtotime($close_date);

    if( $today > $close_date ){
        echo "1";
    } else {
        echo "0";
    }
}

vacancy_expire('someid', '2014-04-30 16:00');

Here is a PHP Fiddle that works for you.

Hope this helps!

Guns
  • 2,678
  • 2
  • 23
  • 51
0

This is the final code

function vacancy_expire($id, $close_date){
        date_default_timezone_set("Pacific/Fiji");
        $today = date("Y-m-d H:m");
        $close_date = trim($close_date);
    if( $today > $close_date ){
       return 0;
    } else {
       return 1;
    }
}
Navneil Naicker
  • 3,586
  • 2
  • 21
  • 31