This question already has answers here:
How to check if a date is in a given range?
(10 answers)
Closed 5 years ago.
I want to check the today's date with specified range of dates and want it to return true if the today's date is between the specified range dates
Something like this:
if (todaysDate is between "2017-04-24 ... 2017-08-30") {
return true;
}
Is there any way to check this in PHP?
Here is some code that may help you, create a function if you want to.
$today= date('Y-m-d');
$today=date('Y-m-d', strtotime($today));;
$date1= date('Y-m-d', strtotime("01/01/2001"));
$date2= date('Y-m-d', strtotime("01/01/2012"));
if (($today> $date1) && ($today< $date2)){
echo "OK !";
}else{
echo "NO OK !";
}
Just create your own method like so:
function isBetweenDates($dateToCheck, $firstDate, $secondDate){
if (($dateToCheck > $firstDate) && ($dateToCheck <
$secondDate))
{
return true;
}
else
{
return false;
}
}
Then call it with dates:
echo isBetweenDates(date('Y-m-d'),strtotime("01/01/2016"),strtotime("01/01/2018"));
Which will return true, because today's date is between 2016 and 2018.
based on: PHP check if date between two dates
Edit:
You could even generalize the function and use it on ints too:
function isBetween($varToCheck, $lowerLimit, $upperLimit){
if (($varToCheck > $lowerLimit) && ($varToCheck <
$upperLimit))
{
return true;
}
else
{
return false;
}
}
Or even make it super specific by converting the input to dates:
function isBetweenDates($dateToCheck, $start_date, $end_date)
{
$start = strtotime($start_date);
$end = strtotime($end_date);
$date = strtotime($dateToCheck);
// Check that user date is between start & end
return (($date > $start) && ($date < $end));
}
Related
I want to make a function that will output a result like this:
//Assuming that today is 2022-01-20
parse_date("2022-01-20") //Today
parse_date("2022-01-19") //Yesterday
parse_date("2022-01-18") //Tuesday
parse_date("2022-01-17") //Monday
parse_date("2022-01-16") //Sunday
parse_date("2022-01-15") //2022-01-15
The idea is to display Today if the date is today, Yesterday if the date is yesterday, the weekday name if the date is within the current week and Y-m-d for anything else.
The current code I have that works is as follows:
public function parse_date($date) {
$carbonDate = Carbon::parse($date);
if($carbonDate->isToday()) return "Today";
if($carbonDate->isYesterday()) return "Yesterday";
$now = Carbon::now();
$start = $now->startOfWeek(CarbonInterface::SUNDAY)->copy();
if($carbonDate >= $start && $carbonDate <= $now->endOfWeek(CarbonInterface::SATURDAY)->copy()) {
return $carbonDate->format('l');
}
return $carbonDate->format('Y-m-d');
}
What I want to know is if there's a better way to do this using other Carbon functions.
Another way to do it is to check the week, and compare it to the current week. Double-check the values first to make sure this works, or if you need to change the locale, with Carbon::parse($date)->locale('en_US');
public function parse_date($date) {
$carbonDate = Carbon::parse($date);
if($carbonDate->isToday()) return "Today";
if($carbonDate->isYesterday()) return "Yesterday";
if($carbonDate->week == Carbon::now()->week) {
return $carbonDate->format('l');
}
return $carbonDate->format('Y-m-d');
}
how to understand if one date in php is less than another minus one day? I mean if for example a date is set to "2018/07/03"; how can I understand if a given date is less than "2018/07/02"
date1 : year1/month1/day1
date2: year2/month2/day2
<?php
if ($year1 >= $year2) {
if ($month1 >= $month2) {
if (($day1 - 1) > $day2) {
echo 'you could do something..';
}
}
}
?>
the above code fails if forexample $year2 = 2017 and $month2 = 11.. can anybody help me? thanks a lot..
Here, this should work.
$date_to_check = new DateTime($yesterday);
$today = new DateTime();
$time_diff = $today->diff($date_to_check)->d;
if($time_diff > 1) {
echo "This is greater than one day.";
}else{
echo "This is not greater than one day.";
$date = strtotime("2018/07/01");
$date2 = strtotime("2018/07/02");
if($date > $date2){
print('date is bigger');
// do stuff when date is bigger than date2
} else {
// else ...
print('date2 is bigger');
}
To convert string to date php has function named strtotime().
Compairing date objects is simple.
There is full information about strtotime()
http://php.net/manual/ru/function.strtotime.php
Another way:
$date = new DateTime("2018/07/01");
$date2 = new DateTime("2018/07/02");
if($date->modify("+1day") > $date2){
print('date is bigger');
// do stuff when date is bigger than date2
} else {
// else ...
print('date2 is bigger or equal');
}
Notice modify modifies $date object itself.
Read more here http://php.net/manual/en/class.datetime.php
I need a function that returns the year when a given date (day + month) occurs the first time from now on.
function year($day, $month) {
// ...
return $year;
}
$day and $year are two-digit numbers
E.g. the given date is '12/25' it should return '2016' (or '16'), but if the date is '02/25' it should return '2017' (or '17').
[Today is August 30, 2016]
Leap years may be disregarded and input doesn't have to be validated.
EDIT:
My attempt
year($day, $month) {
$today_day = date('d');
$today_month = date('m');
$year = date('y');
if($month > $today_month) {
return $year;
} else if ($month < $today_month) {
return $year + 1;
} else {
if($day >= $today_day) {
return $year;
} else {
return $year + 1;
}
}
}
Just compare the date you are checking against today. If it is today or earlier increment the year of the date. Otherwise do not. Then return that year.
DateTime() functionality makes this easy to do.
function year($day, $month) {
$date = new DateTime("{$month}/{$day}"); // defaults to current year
$today = new DateTime();
if ($date <= $today) {
$today->modify('+1 year');
}
return $today->format('Y');
}
echo year(6, 6); // 2017
echo year(12, 12); // 2016
Demo
I appreciate your effort! It was pretty good, but can certainly use some fine tuning. We could reduce the no. of unnecessary if statements.
The function accepts two parameters: month and date. Please be sure we follow the order while calling the function.
In the function, $date is the input date concatenated with the current year.
E.g: year(12,25) refers to the year where month is December (12) and day is 25.
year(12,25) would make $date as 2015-12-25.
function year($month, $day)
{
$date= date('Y').'-'.$month.'-'.$day;
if (strtotime($date) > time()) {
return date('y');
}
return (date('y')+1);
}
echo year(12,25); // 16
echo year(2,25); // 17
Now, all we need to do is check the timestamp of $date with the current timestamp- time().
strtotime($date) > time() input date timestamp is more than current timestamp. Which implies this date is yet to come in this year. So, we return the current year date('Y').
If the above if is not executed, it's obvious that this date has passed. Hence we return the next year date('Y') + 1.
This question already has answers here:
How can I check if the current date/time is past a set date/time?
(4 answers)
Closed 9 years ago.
How can I check if the date is later than the current date?
if(date("d") < "18" && date("m") < "01") {
echo 'To late!';
}
Doesn't work for me.
You can do this notation:
if( '20140505' > date("Ymd") ) {
// today's date is before 20140505 (May 5, 2014)
}
$time1 = strtotime(date("d/m/Y", "18/01/2014"));
if(time() > $time1)
{
echo "too late!";
}
The best is to use strtotime.
$current_date = date("Y-m-d");
$date_to_compare = date("Y-m-d",time()+86400); //1 day later
if (strtotime($date_to_compare) > strtotime($current_date)) {
echo "too late";
}
This question already has answers here:
How can I check if the current date/time is past a set date/time?
(4 answers)
Closed 9 years ago.
My date is being returned like 01/05/2013 12:44. How do I check in php if this datetime is in the future?
Try this:
<?php
$dt = '01/05/2013 12:44';
$nowdt = time();
$diff = strtotime($dt) - $nowdt;
echo $diff;
if($diff > 0){
echo (" your date is future date");
} else {
echo ("your date is is not future date");
}
?>
You can compare time() with the UNIX timestamp representation of that date:
if (time() < strtotime("01/05/2013 12:44"))
{
// your time is greater than todays date
}
Another way
$time = strtotime(str_replace("/","-",$date) )
if ($time > time())
{
echo "$date is in the future."
}
From the PHP manual use mktime.
Example in pseudo code:
$today = mktime("today");
$your_date = mktime("...");
if ($your_date > $today) {
// it's in the future
}
Convert to Unix time, then see if it's greater than the current time.
<?php
if(strtotime("01/05/2013 12:44") > time()) {
//time is in the future.
}else{
//time is in the past
}
?>