PHP Date Between [duplicate] - php

This question already has answers here:
How can I determine if a date is between two dates in PHP?
(3 answers)
Closed 9 years ago.
Need to check wether a given date is in between two given dates using PHP, This is the code I've written:
$today = date_create('28-Feb-2012');
$fromdate = date_create('20-Feb-2012');
$todate = date_create('22-Feb-2012');
if ($today>=$fromdate && $todat<=$todate)
{
echo 'in range';
}
else
{
echo 'not in range';
}
But this always returns in range. Can't figure out where the problem is. Please help.

I know it's a bit of a hacky solution, but I tend to use strtotime() for that kind of thing, as it returns an integer:
$today = time();
$fromdate = strtotime('20-Feb-2012');
$todate = strtotime('22-Feb-2012');
if ($today>=$fromdate && $today<=$todate)
{
echo 'in range';
}
else
{
echo 'not in range';
}
EDIT: i assume the $todat was just a typo into stackoverflow, and that in your code you actually have $today, but if not that might be your problem - undefined variables, in some settings, can be considered 0 I believe.

Related

compare date string in php with mm-dd-yy format [duplicate]

This question already has answers here:
How can I compare two dates in PHP?
(13 answers)
Closed 5 years ago.
i need to compare two date from string,
my dates:
first date: 11-11-19
second date: 11-24-17
so i try to
$firstdate = "11-11-19";
$seconddate = "11-24-17";
if($firstdate < $seconddate)
{
echo "firstdate is minor than the secondate";
}
else
{
echo "seconddate is major than the firstdate";
}
if i change < or > the if statement should change, but i get always the firsdate...
how to do to compare two dates in this forma mm-dd-yy ?
Thanks
You can use strtotime to convert string to unix timestamp, and compare that.
$firstdate = strtotime("11-11-19");
$seconddate = strtotime("11-24-17");
if($firstdate < $seconddate)
{
echo "firstdate is minor than the secondate";
}
else
{
echo "seconddate is major than the firstdate";
}
You can convert both dates to timestamp:
echo (strtotime($firstdate) < strtotime($seconddate)) ? "firstdate is minor than the secondate" : "seconddate is major than the firstdate";

How to compare two times using php [duplicate]

This question already has answers here:
How to compare two time in PHP
(11 answers)
Closed 8 years ago.
I have to compare time with a time value stored in a variable. I have devised the logic but it fails at time.
Following is the logic:
$diff = "22:32:53";
if ($diff > strtotime("072:00:00") )
{
echo "DO A";
}
else if ($diff < strtotime("08:00:00"))
{
echo "DO B";
}
else if( $diff > strtotime("08:00:00") && $diff < strtotime("072:00:00"))
{
echo "DO C";
}
But it echoes "DO A" whereas it should display "DO C". Could not figure out where I am missing the logic
Thanks in advance.
If you want to compare time (without date), the best way is to manually convert them into seconds, and compare the seconds. strtotime may work, but you'll need to run it on $diff too before comparing.
First thing, $diff is a string not an int, it should be:
$diff = strtotime("22:32:53");
Comparing is usually done using DateTime::diff
The comparison will not work anyway because if you check
var_dump(strtotime("072:00:00"));
will return:
false
strtotime is trying to match the format against a known time format, and 72 is not a valid hour.

PHP if condition based on date not working [duplicate]

This question already has answers here:
Compare given date with today
(14 answers)
Closed 8 years ago.
I want to execute a if statement where I want if given {date} is less or equal than today then {due_amount}= {paidbystudent} else {due_amount}= '0'
Here is my code:
if ({date} <= Today()) {
{due_amount}={paidbystudent};
} else {
{due_amount}='0';
}
But its not working. Please help me how to do this.
I have also tried this code but its not working properly this is checking only date and ignoring month and year
$todays_date=date("d/m/Y", strtotime("today"));
$date=date("d/m/Y", strtotime({date}));
if ($date <= $todays_date) {
{due_amount}={paidbystudent};
} else {
{due_amount}='0';
}
How about this:
$date = '2014-05-22'; // fetch from db, user input etc
if (strtotime($date) <= time()) {
echo 'paid by student';
} else {
echo 0;
}
DEMO
EDIT: As pointed out by deceze, a better way to approach this would be to compare the UNIX timestamp values instead of a date format.

How do I compare dates in PHP? [duplicate]

This question already has answers here:
How to compare two dates in php [duplicate]
(16 answers)
Closed 8 years ago.
How can I check if a date captured in the database is less than the current date?
foreach( $results as $r ) {
$date = $r['date'];
if($date < $currentdate) {
echo 'oldest dates';
} else {
echo 'newest dates';
}
Use strtotime function:
$date = strtotime($date);
$currentdate = strtotime($currentdate);
Use strtotime($date)<strtotime($currentdate)
and make sure that both dates are in correct format.

Odd Date("Y-m-d") result

Im having some bizarre results in regards to the php date() function. Basically Im getting a date from a Mysql database which is in a string format, split into three elements. This would be Day, Month, Year (15 september 2012 for example) Im ultimately comparing two dates to see if it has expired. But the issue is that only certain dates are allowing the code to work, and some do not work at all (or allow the if statement to work effectively) Below is my code, any help would be great.
$today = date("d-m-Y");
$expire = date("d-m-Y",strtotime($this->getData('date_day')."-".
$this->getData('date_month')."-".$this->getData('date_year'))) ;
if ($expire < $today)
{
echo 'expired';
}
else
{
echo 'Not expired';
}
Im sure its something simple, but for some reason I cannot solve it.
You need to compare the Unix timestamps.
$today = time();
$expire = strtotime($this->getData('date_day')."-".
$this->getData('date_month')."-".$this->getData('date_year')) ;
if ($expire > $today)
{
echo 'expired';
}
else
{
echo 'Not expired';
}
It looks like strtotime is expected a US date format; you need to swap the month and the day around to generate a valid date:
$today = date("d-m-Y");
$expire = date("d-m-Y",strtotime($this->getData('date_month')."-".
$this->getData('date_day')."-".$this->getData('date_year'))) ;
On the other hand, see Stephen305's answer - it's a much better solution to your problem.

Categories