1

here i have stored two dates in db like start date as 2014-07-07 00:00:00 and end date as 2014-07-15 23:59:59. Now how can I check my current date between the two days

$date1 = '2014-07-07 00:00:00';
$date2 = '2014-07-15 23:59:59';

$now = new DateTime();
$current_time = $now->format('Y-m-d H:i:s');?>

if date1 and date2 are retrieved from db and compare with current date it getting from server if it is between the two days it should be display end time from now.

5 Answers 5

1

USE :

$date1 = '2014-07-07 00:00:00';
$date2 = '2014-07-15 23:59:59';

$cdate1 = new DateTime($date1);
$vdate1 = $cdate1->getTimestamp();

$cdate2 = new DateTime($date2);
$vdate2 = $cdate1->getTimestamp();

compare integers ($vdate1 and $vdate2) to each other
Result will be in seconds

Enjoy :)


Sign up to request clarification or add additional context in comments.

Comments

1

Just as an Object Oriented style alternative you can create objects as instances of DateTime class, like these:

$date1 = new DateTime('2014-07-07 00:00:00');
$date2 = new DateTime('2014-07-15 23:59:59');
$now = new DateTime();

then, following your logic, you can compare if now is before or after the other dates, like:

var_dump($date2 > $now);

or you can retrieve an instance of DateInterval interface with:

$now_interval_from_date1 = $date1->diff($now);

and then use the format method to know exactly the time/day/etc.. differences, like:

$now_interval_from_date1->format("%R%H hours")

You can find the format params here: http://www.php.net/manual/it/dateinterval.format.php

Comments

1

If the two dates are exactly in the same format, php allows string comparison.

So you can do the following:

if(strcmp($date1, $current_time) <= 0 and strcmp($date2, $current_time) > 0)
{
// The date is within the limits
}

strcmp is a safer function for string comparison

So, in your case, you could have the following:

<?php 
    $con=mysqli_connect("localhost","root","","proms"); // Check connection
    if (mysqli_connect_errno()) { 
        echo "Failed to connect to MySQL: " . mysqli_connect_error();
    }

    date_default_timezone_set("America/New_York");
    $current_time =date("h:i:sa");
    $result = mysqli_query($con,"SELECT * FROM image");
    while($row = mysqli_fetch_array($result)) { 
        if(strcmp($row['start_date'],$current_time) <= 0 && strcmp($row['end_date'],$current_time) > 0) {
            // The date is within the limits
            echo "yes";
        }
    }

?>

'start_date' and 'end_date' should be substituted with your fields' names.

4 Comments

how to $date1 and $date2 are i entered manually but in my db it was more then 100 how i loop that for those
It's the same comparison, just within a for/while loop, and putting in place of $date1 and $date2 the right variable name (e.g. $data["date_start"] and $data["date_end"], where $data is the variable where you loaded the single row). I hope this helps! :)
<?php $con=mysqli_connect("localhost","root","","proms"); // Check connection if (mysqli_connect_errno()) { echo "Failed to connect to MySQL: " . mysqli_connect_error(); } date_default_timezone_set("America/New_York"); $current_time =date("h:i:sa"); $result = mysqli_query($con,"SELECT * FROM image"); while($row = mysqli_fetch_array($result)) { /*foreach($row as $rows){*/ if($row['start_date'] <= $current_time || $row['end_date'] > $current_time) { // The date is within the limits echo "yes"; } /* }*/ } ?>
for one i did this but more then one it now allowing
0

You can do as following to compare current time between two dates

$date1 = '2014-07-07 00:00:00';
$date2 = '2014-07-15 23:59:59';
$now = date('Y-m-d H:i:s');

$date1TimeStamp = strtotime($date1);
$date2TimeStamp = strtotime($date2);
$nowTimeStamp = strtotime($now);

if($date1TimeStamp <= $nowTimeStamp || $date2TimeStamp >= $nowTimeStamp){
   $seconds_diff = $date2TimeStamp - $nowTimeStamp;
   $days = $seconds_diff/(3600*24);
}else
   echo 'No, out';

Edited calculation, now it displays remaining days time from now to end date.

Comments

0
if(strtotime('now') > strtotime($date1) && strtotime('now') < strtotime($date2)) {
       $diff = strtotime($date2) - strtotime('now');
} 

$diff then contains the difference between the two dates in miliseconds. Just convert miliseconds to days : hours : minutes (Mathematics ?)

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.