PHP date function date range [duplicate] - php

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
PHP, see if date range is partly within another date range
I have 2 dates, a start date (2011-01-01) and an end date (2011-02-28).
I have a third date that starts on 2010-01-01 and ends on 2012-03-01. This means that this third date falls within the first 2 dates range.
If the third dates start and/or end date is not within the 2 dates then it must be false.
How can I check this using php?

You can do the following:
$startDate = strtotime('2011-01-01');
$endDate = strtotime('2011-02-28');
$intervalStart = strtotime('2011-02-01');
$intervalEnd = strtotime('2012-03-01');
if ($startDate < $intervalStart || $endDate > $intervalEnd) {
echo 'Not in the interval';
} else {
echo 'In the interval';
}

Related

PHP - how do I check if today's date matches date stored in database? [duplicate]

This question already has answers here:
Use MySQL to determine whether today is a user's birthday
(16 answers)
Closed 3 years ago.
PHP. I'm trying to compare two dates. One is dateofbirth stored in a MySQL table and the other is today's date without the year. if the date (m-d) stored in database matches today's date (m-d) a window will pop up.
This is for a Linux server, running MySQL 5, PHP 5.
$sql1 = "SELECT * FROM perfil_aluno WHERE id_usuarios =
'".$_SESSION['usuarioId']."' ";
$query1 = mysqli_query($mysqli, $sql1);
$dados1 = mysqli_fetch_assoc($query1);
$dob = date('m-d', strtotime($dados1['dateofbirth']));
$2day = date('m-d');
if($dob == $2day) {
// execute some code...
}
If the month and day of birth (m-d) stored in the database matches today's date month and day, a window will pop up wishing the user a Happy Birthday.
You could use the DateTime objects to compare the dates like so:
$date1 = DateTime::createFromFormat('!m-d', $dob);
$date2 = DateTime::createFromFormat('!m-d', $2day);
if ($date1 == $date2) {
// execute some code...
}
The ! prevents taking the current timestamp instead of 00:00:00 so it would be safer to use it in a comparision without the time part needed. See in the comments here

PHP DateTime diff returns wrong difference [duplicate]

This question already has answers here:
Using DateTime::diff() to return days between two dates
(2 answers)
Closed 4 years ago.
I calculate the difference of to queried dates. When the difference is lower than one month everything is correct. Here a example of a wrong result:
Query output:
$row['start'] = '2018-08-06';
$row['end'] = '2018-09-26';
Code:
$start = new DateTime($row['start']);
$end = new DateTime($row['end']);
$days = $start->diff($end)->format("%d");
Output:
$days = 20;
The DateTime difference is correct and the error only comes in when you are formatting the output. %d is the day of the month, not the total days in the DateInterval. These two datetimes are 1 month and 20 days apart. So %d only shows the 20 days part. %a should get you the total number of days.
A full guide to the format can be found here:
http://php.net/manual/en/dateinterval.format.php

How to slice a date so, i can compare with month and day only? [duplicate]

This question already has answers here:
How to compare two dates without years in php? [duplicate]
(3 answers)
Closed 5 years ago.
I have a problem with date .
I have a Birth date eg. 1993-06-01 in database. And now i want to compare with now date so, I can pop up the birthday message. How is this possible to compare with only month and day field. Especially in query so i can attached their name with birthday message
Used framework Pyrocms.
For PHP just use DateTime.
Example:
$date1 = new DateTime('1993-06-01');
$date2 = new DateTime();
if ($date1->format('m-d') === $date2->format('m-d')) {
// send the birthday message
}
If you want to do this in SQL then use MONTH() and DAY().
Example:
$sql = "SELECT * FROM `table` WHERE MONTH(`date_column`) = " . $date->format('n') . " AND DAY(`date_column`) = " . $date->format('j');
If you need to compare with month, you have this function in mysql:
MONTH(date)
That returns the month (in numbers 1 .. 12)
You also have
MONTHNAME(date)
that returns the month in string ('January', ' February', ...)
For the day you have:
DAYNAME(date) --> returns ('Sunday','Monday' ...)
DAYOFWEEK(date) --> returns (1 = Sunday, 2 = Monday, …, 7 = Saturday)
You can look for all the date time functions in this link MySql dev

PHP - comparing two dates [duplicate]

This question already has answers here:
Compare given date with today
(14 answers)
Closed 8 years ago.
I want check whether the date from variable is older than current date about 7 days or more.
Now, I check whether this date is -1 day from current:
$old_email = strtotime($result->repairs_date_received) >= strtotime('-1 day') ? true : false;
UNIX time is in seconds, so you can simply check using basic operators like > and <.
$week_ago = strtotime('a week ago');
$check = strtotime($result->repairs_date_received);
if($check > $week_ago){
// date is newer than week
} else {
// date is older than week
}

Compare Dates and find out weather First date is greater from second [duplicate]

This question already has answers here:
How to compare two dates in php [duplicate]
(16 answers)
Closed 9 years ago.
Date1=23-3-2013
Date2=26-3-2013
How to compare above both date such as
If Date2 >= Date1
Echo greater them equal to
Else
Echo not greater them equal to
Here greater does not means mathematically greater
Use the function strtotime($date)
Therefore, you would have something like:
if(strtotime($date1) < strtotime($date2)){
echo Date 2 is greater than date 1;
}else{
echo Date 1 is greater or equal than Date 2;
}

Categories