PHP if statement compare 3 dates - php

I am having trouble writing if statement
there are the 3 variables
$date = 1985-11-01;
$date2 = 2005-11-08;
$date3 = 2006-11-08;
and here is my if statement.
if($date > $date2 && $date < $date3) {
// dob is between the limits
return TRUE;
}
else {
// dob is outside the limits
return FALSE;
}
What I am try to do is, if $date is not in between $date2 and $date3, return false. I am very tired today and my brain is not working, can someone tell me what I am doing wrong?

You can use strtotime to ensure you compare correctly.
$date = strtotime('1985-11-01'); //499680000
$date2 = strtotime('2005-11-08'); //1131436800
$date3 = strtotime('2006-11-08'); //1162972800
When you do your logic, look at the Unix generated timestamps...

convert your variables to time objects using strtotime
$my_date = strtotime('08/11/2012');

//using strtotime convert your date(s) in time stamp then your checking will be correctly worked.
$date = strtotime('1985-11-01');
$date2 = strtotime('2005-11-08');
$date3 = strtotime('2006-11-08');
if($date > $date2 && $date < $date3)
return true;
else
return false;

Related

Unable to correctly calculate date with "0000-00-00" date in php

I have two date like this
$date1 = "2018-11-07";
$date2 = "2018-11-12";
I want to compare these two date so i could get a range of day between these date. I used DiffDate function. It's looks like below syntax.
$datediff1 = new DateTime($date1);
$datediff2 = new DateTime($date2);
$interval = $datediff2->diff($datediff1);
echo $interval->d; // will return 5
I got the function, but i have problem when one from these two date had a value like "0000-00-00". They started to looks like this.
$date1 = "2018-11-07";
$date2 = "0000-00-00";
$datedif1 = new DateTime($date1);
$datediff2 = new DateTime($date2);
$interval = $datediff2->diff($datediff1);
echo $interval->d; // will return 8
//// If i reverse the date like $datediff1->diff($datediff2);
//// it will return 7 instead
My question is how could i prevent these two dates return 0 when one or all dates have "0000-00-00" value ? I have tried like using if...else....
if($date1 == "0000-00-00" || $date2 == "0000-00-00"){
$interval = "0";
echo $interval;
}else {
$interval = $date2->diff($date1);
$i = $interval->d;
}
Is there any better way to do it rather than using if else statement ?
SEE IT ONLINE HERE
PS : I am using PHP 5.6
You can rely on the fact that the timestamp starts with 1970 so a 0000 year would result a negative timestamp :
$date1 = "2018-11-07";
$date2 = "0000-00-00";
$datedif1 = new DateTime($date1);
$datediff2 = new DateTime($date2);
// check if strtotime returns a valid timestamp and the year is not 0000
$interval = (!strtotime($date1) ||
!strtotime($date2) || strtotime($date1) < 0 ||
strtotime($date2) < 0) ? 0 : $date2->diff($date1);
print_r($interval);
This needs to be refined because it won't work in all cases but it's working in your case.

Compare datetime with 0 in laravel

These two datetime string are equal. How can we compare this two string datetime with laravel.
$str_date1='08/09/2017';
$str_date2='8/9/2017';
How to compare these two string datetime by ignore 0 with laravel.
To achieve what you are trying you need Carbon class, you can try something like this:
$date1 = Carbon\Carbon::parse($str_date1)->toDateTimeString();
$date2 = Carbon\Carbon::parse($str_date2)->toDateTimeString();
if($date1 == $date2)
{
// Do your code
}
Hope this helps
You should try this:
$str_date1='08/09/2017';
$str_date2='8/9/2017';
$date1 = strtotime($str_date1);
$date2 = strtotime($str_date2);
if($date1 == $date2){
//Your code
}
Updated answer:
$str_date1='30/08/2017';
$str_date1 = str_replace('/', '-', $str_date1);
$str_date2='30/10/2017';
$str_date2 = str_replace('/', '-', $str_date2);
$date1 = strtotime($str_date1);
$date1 = date('m/d', $date1);
$date1 = strtotime($date1);
$date2 = strtotime($str_date2);
$date2 = date('m/d', $date2);
$date2 = strtotime($date2);
if(date1 == $date2){
//Your code
}
Cast them to DateTime and then compare. No need to use anything like Carbon or any fancy class, or helper.
$str_date1 = '08/09/2017';
$str_date2 = '8/9/2017';
$date1 = \DateTime::createFromFormat('d/m/Y', $str_date1);
$date2 = \DateTime::createFromFormat('d/m/Y', $str_date2);
if ($date1 > $date) {
// Your stuff
}
//Modifys to set same dates
$date1 = new DateTime('08/07/2017')->modify('d-m-Y');
$date2 = new DateTime('8/7/2017')->modify('d-m-Y');
//Difference In Days
$dateDifference = $date1->diff($date2)->format("%a");
Depending on your needs, PHP comes shipped with the ability to differentiate between dates. http://php.net/manual/en/class.datetime.php

Convert to Date on php it's not working

I have the following code:
$date1 = (string)$_POST['convocatory_open_start_date']; // "30/04/2015"
$date2 = (string)$_POST['convocatory_open_end_date']; // "31/05/2015"
$startDate = date('Y-m-d', strtotime($date1));
$endDate = date('Y-m-d', strtotime($date2));
but I always get $startDate and $endDate 1970-01-01 Why???
Please Help I have like 3 hours in the same problem.
Thanks
The problem is that if you use / as a separator, strtotime will assume the format is m/d/Y. So it will not be able to convert it, and it defaults to 1970-01-01.
The easiest solution would be
$startDate = DateTime::createFromFormat('d/m/Y', $date1)->format('Y-m-d');
$endDate = DateTime::createFromFormat('d/m/Y', $date2)->format('Y-m-d');
First check if the posted variables :-
$date1 and $date12 are not NULL
Because NULL is interpreted as 0 by the php function strtotime(), since you are supposed to pass an integer timestamp. A timestamp of 0 means 1-1-1970.
You need to check the posted contents of the posted variables
if(($date1 === NULL) || ($date1 === NULL) ) {
//don't use `strtotime()`
}
If that is not the case and Your variables are posted, the only thing i can think of is that you have problem with the stringTimeFormatting when using the date function and you input format.
$date1 = (string)$_POST['convocatory_open_start_date']; // "30/04/2015"
$date2 = (string)$_POST['convocatory_open_end_date']; // "31/05/2015"
$startDate = date($date1);
$endDate = date($date2);
echo $startDate, '<br/> ' , $endDate;
Hope that adds some to your Quest!

comparing time in php error

i'm trying to compare the current time against 5 other times as a schedule but it's not working. Dates are fetched from the database here's the code
$now = new DateTime();
$date1 = new DateTime($pRow['date1']);
$date2 = new DateTime($pRow['date2']);
dates in database:
date1: 12:00
date2: 03:25
if($now->format('h:i') >= $date1->format('h:i') && $now->format('h:i') <= $date2->format('h:i')){
echo "date2";
}
else{
echo "no";
}
the result is always no (code edited to be simple and faster to solve the problem)
if($now >= $date5 && $now <= $date5)
wrong data is comparing
When using the h modifier for time you are working with only 1-12 for hours. This means 2pm is just 2 and 11am is 11. Therefore 11 > 2 which breaks your math. Use H instead.
$now = date("H:i",strtotime("now"));
$date1 = date("H:i",strtotime($pRow['date1']));
$date2 = date("H:i",strtotime($pRow['date2']));
$date3 = date("H:i",strtotime($pRow['date3']));
$date4 = date("H:i",strtotime($pRow['date4']));
$date5 = date("H:i",strtotime($pRow['date5']));
Or, better yet, use DateTime which are comparable.
$now = new DateTime();
$date1 = new DateTime($pRow['date1']);
$date2 = new DateTime($pRow['date2']);
$date3 = new DateTime($pRow['date3']);
$date4 = new DateTime($pRow['date4']);
$date5 = new DateTime($pRow['date5']);
This code was already at stackoverflow, try to use it :)
$ThatTime ="14:08:10";
if (time() >= strtotime($ThatTime)) {
echo "The Time that was entered is behind";
}

how to compare my current time and date with db stored date

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.
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 :)
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
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.
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.
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 ?)

Categories