Imrul.H
Imrul.H

Reputation: 5870

PHP check if today is in between two dates

Consider following code:

$today = date("d/m/Y");

$start_date = '20/11/2014';
$time1 = strtotime($start_date1);
$start_date1 = date('d/m/Y',$time1);

$end_date = '20/01/2015';
$time2 = strtotime($end_date1);
$end_date1 = date('d/m/Y',$time2);

if( $start_date1 <= $today && $end_date1 >= $today)
    echo "yes";
else
    echo 'no';

Even though $today is in between those start and end date, I get "no" in return. What might be the problem here? I just wanna check if today is in between those dates. Start date and end date are saved as string in DB.

Upvotes: 3

Views: 22006

Answers (1)

goldlife
goldlife

Reputation: 1989

Try this:

<?php

    $now = new DateTime();
    $startdate = new DateTime("2014-11-20");
    $enddate = new DateTime("2015-01-20");

    if($startdate <= $now && $now <= $enddate) {
        echo "Yes";
    }else{
        echo "No";
    }

?>

Upvotes: 12

Related Questions