PHP check if date between two dateshow to know if a date/time falls in some date/time range?How do I check to see if action occurred up to 5 days before today in PHP?php how to check if date string in between datesPHP specific time period functionPHP if between 2 datesCheck today's date with specified date range with PHPIF todays date is between 1st December and 5th JanuaryWarning range() : The supplied range exceeds the maximum array size: start=0 end=1508715000Check if time has crossed PHPdate within range or notHow can I prevent SQL injection in PHP?Deleting an element from an array in PHPCompare two dates with JavaScriptDetecting an “invalid date” Date instance in JavaScriptHow do I get the current date in JavaScript?Calculate difference between two dates (number of days)?How to format a JavaScript dateReference — What does this symbol mean in PHP?How do I check if a string contains a specific word?Why is subtracting these two times (in 1927) giving a strange result?
What can make Linux unresponsive for minutes when browsing certain websites?
Is there a loss of quality when converting RGB to HEX?
How would I as a DM create a smart phone-like spell/device my players could use?
Unexpected route on a flight from USA to Europe
Will a paper be retracted if a flaw in released software code invalidates its central idea?
Could one become a successful researcher by writing some really good papers while being outside academia?
How can glass marbles naturally occur in a desert?
Can a PC attack themselves with an unarmed strike?
How does The Fools Guild make its money?
Why couldn't soldiers sight their own weapons without officers' orders?
What does Fisher mean by this quote?
How does the oscilloscope trigger really work?
Why can I log in to my Facebook account with a misspelled email/password?
Is Odin inconsistent about the powers of Mjolnir?
Does the length of a password for Wi-Fi affect speed?
Is this cheap "air conditioner" able to cool a room?
Short story about a teenager who has his brain replaced with a microchip (Psychological Horror)
Sets A such that A+A contains the largest set [0,1,..,t]
What word can be used to describe a bug in a movie?
Traveling from Germany to other countries by train?
If there were no space agencies, could a person go to space?
Word or idiom defining something barely functional
Whats the name of this projection?
Why is there a need to prevent a racist, sexist, or otherwise bigoted vendor from discriminating who they sell to?
PHP check if date between two dates
how to know if a date/time falls in some date/time range?How do I check to see if action occurred up to 5 days before today in PHP?php how to check if date string in between datesPHP specific time period functionPHP if between 2 datesCheck today's date with specified date range with PHPIF todays date is between 1st December and 5th JanuaryWarning range() : The supplied range exceeds the maximum array size: start=0 end=1508715000Check if time has crossed PHPdate within range or notHow can I prevent SQL injection in PHP?Deleting an element from an array in PHPCompare two dates with JavaScriptDetecting an “invalid date” Date instance in JavaScriptHow do I get the current date in JavaScript?Calculate difference between two dates (number of days)?How to format a JavaScript dateReference — What does this symbol mean in PHP?How do I check if a string contains a specific word?Why is subtracting these two times (in 1927) giving a strange result?
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
I got this code from Stackoverflow and changed it slightly to work with today's date.
I want to check if today fits between two dates. But this is not working.
What am I missing?
$paymentDate = date('d/m/Y');
echo $paymentDate; // echos today!
$contractDateBegin = date('d/m/Y', '01/01/2001');
$contractDateEnd = date('d/m/Y', '01/01/2015');
if ($paymentDate > $contractDateBegin && $paymentDate < $contractDateEnd)
echo "is between";
else
echo "NO GO!";
php date
add a comment |
I got this code from Stackoverflow and changed it slightly to work with today's date.
I want to check if today fits between two dates. But this is not working.
What am I missing?
$paymentDate = date('d/m/Y');
echo $paymentDate; // echos today!
$contractDateBegin = date('d/m/Y', '01/01/2001');
$contractDateEnd = date('d/m/Y', '01/01/2015');
if ($paymentDate > $contractDateBegin && $paymentDate < $contractDateEnd)
echo "is between";
else
echo "NO GO!";
php date
1
if youre on php5, check out datetime.diff on no2.php.net/manual/en/datetime.diff.php
– Tom
Sep 28 '13 at 18:12
Do you really mean to use date() or strtotime()?
– Mark Baker
Sep 28 '13 at 18:12
I used strtotime and still got a "No Go!";
– Papa De Beau
Sep 28 '13 at 18:15
Then you should read the formats that strtotime() accepts.... but date() is definitely the wrong function to use
– Mark Baker
Sep 28 '13 at 18:17
Why a -1? This seems to be a good question. I placed my code and it has got some good comments. I think this could be helpful to someone else.
– Papa De Beau
Sep 28 '13 at 18:21
add a comment |
I got this code from Stackoverflow and changed it slightly to work with today's date.
I want to check if today fits between two dates. But this is not working.
What am I missing?
$paymentDate = date('d/m/Y');
echo $paymentDate; // echos today!
$contractDateBegin = date('d/m/Y', '01/01/2001');
$contractDateEnd = date('d/m/Y', '01/01/2015');
if ($paymentDate > $contractDateBegin && $paymentDate < $contractDateEnd)
echo "is between";
else
echo "NO GO!";
php date
I got this code from Stackoverflow and changed it slightly to work with today's date.
I want to check if today fits between two dates. But this is not working.
What am I missing?
$paymentDate = date('d/m/Y');
echo $paymentDate; // echos today!
$contractDateBegin = date('d/m/Y', '01/01/2001');
$contractDateEnd = date('d/m/Y', '01/01/2015');
if ($paymentDate > $contractDateBegin && $paymentDate < $contractDateEnd)
echo "is between";
else
echo "NO GO!";
php date
php date
asked Sep 28 '13 at 18:09
Papa De BeauPapa De Beau
1,78716 gold badges69 silver badges127 bronze badges
1,78716 gold badges69 silver badges127 bronze badges
1
if youre on php5, check out datetime.diff on no2.php.net/manual/en/datetime.diff.php
– Tom
Sep 28 '13 at 18:12
Do you really mean to use date() or strtotime()?
– Mark Baker
Sep 28 '13 at 18:12
I used strtotime and still got a "No Go!";
– Papa De Beau
Sep 28 '13 at 18:15
Then you should read the formats that strtotime() accepts.... but date() is definitely the wrong function to use
– Mark Baker
Sep 28 '13 at 18:17
Why a -1? This seems to be a good question. I placed my code and it has got some good comments. I think this could be helpful to someone else.
– Papa De Beau
Sep 28 '13 at 18:21
add a comment |
1
if youre on php5, check out datetime.diff on no2.php.net/manual/en/datetime.diff.php
– Tom
Sep 28 '13 at 18:12
Do you really mean to use date() or strtotime()?
– Mark Baker
Sep 28 '13 at 18:12
I used strtotime and still got a "No Go!";
– Papa De Beau
Sep 28 '13 at 18:15
Then you should read the formats that strtotime() accepts.... but date() is definitely the wrong function to use
– Mark Baker
Sep 28 '13 at 18:17
Why a -1? This seems to be a good question. I placed my code and it has got some good comments. I think this could be helpful to someone else.
– Papa De Beau
Sep 28 '13 at 18:21
1
1
if youre on php5, check out datetime.diff on no2.php.net/manual/en/datetime.diff.php
– Tom
Sep 28 '13 at 18:12
if youre on php5, check out datetime.diff on no2.php.net/manual/en/datetime.diff.php
– Tom
Sep 28 '13 at 18:12
Do you really mean to use date() or strtotime()?
– Mark Baker
Sep 28 '13 at 18:12
Do you really mean to use date() or strtotime()?
– Mark Baker
Sep 28 '13 at 18:12
I used strtotime and still got a "No Go!";
– Papa De Beau
Sep 28 '13 at 18:15
I used strtotime and still got a "No Go!";
– Papa De Beau
Sep 28 '13 at 18:15
Then you should read the formats that strtotime() accepts.... but date() is definitely the wrong function to use
– Mark Baker
Sep 28 '13 at 18:17
Then you should read the formats that strtotime() accepts.... but date() is definitely the wrong function to use
– Mark Baker
Sep 28 '13 at 18:17
Why a -1? This seems to be a good question. I placed my code and it has got some good comments. I think this could be helpful to someone else.
– Papa De Beau
Sep 28 '13 at 18:21
Why a -1? This seems to be a good question. I placed my code and it has got some good comments. I think this could be helpful to someone else.
– Papa De Beau
Sep 28 '13 at 18:21
add a comment |
11 Answers
11
active
oldest
votes
Edit: use <= or >= for count todays date
This is the right answer for your code.. Just use strtotime() php function
$paymentDate = date('Y-m-d');
$paymentDate=date('Y-m-d', strtotime($paymentDate));
//echo $paymentDate; // echos today!
$contractDateBegin = date('Y-m-d', strtotime("01/01/2001"));
$contractDateEnd = date('Y-m-d', strtotime("01/01/2012"));
if (($paymentDate >= $contractDateBegin) && ($paymentDate <= $contractDateEnd))
echo "is between";
else
echo "NO GO!";
Thank you good sir! Maybe it might be good to remove your answer that you added at the top and just leave the one that works for me. Not sure.
– Papa De Beau
Sep 28 '13 at 18:35
4
@PapaDeBeau: You have to keep in mind that those01/01/2001
dates in this solution are inm/d/Y
format, notd/m/Y
.
– Jon
Sep 28 '13 at 20:43
5
I would not use stric comparison, so this is the best $paymentDate >= $contractDateBegin and $paymentDate <= $contractDateEnd
– pollux1er
Aug 14 '14 at 8:39
if (($paymentDate > $contractDateBegin) && ($paymentDate < $contractDateEnd)) can be if (($paymentDate >= $contractDateBegin) && ($paymentDate <= $contractDateEnd)) So that it can check greater than equal to and less than equal to.
– Kamesh Jungi
Jul 15 '15 at 11:18
1
This solution, and every other solution on this page fails when you provide a date that starts before the unix epoch. Try it with 1942/07/28 and see what happens.
– Alex Barker
Jan 4 '16 at 20:53
|
show 3 more comments
You cannot compare date-strings. It is good habit to use PHP's DateTime
object instead:
$paymentDate = new DateTime(); // Today
echo $paymentDate->format('d/m/Y'); // echos today!
$contractDateBegin = new DateTime('2001-01-01');
$contractDateEnd = new DateTime('2015-01-01');
if (
$paymentDate->getTimestamp() > $contractDateBegin->getTimestamp() &&
$paymentDate->getTimestamp() < $contractDateEnd->getTimestamp())
echo "is between";
else
echo "NO GO!";
Again, it must be a php5 issue. I guess I need to update it with godaddy. I got this error: Fatal error: Call to undefined method DateTime::getTimestamp()
– Papa De Beau
Sep 28 '13 at 18:24
What PHP version are you using?
– luttkens
Sep 28 '13 at 18:25
@PapaDeBeau: It's not available on PHP versions older than 5. What does<?php echo phpversion(); ?>
output?
– Amal Murali
Sep 28 '13 at 18:26
1
Looks like getTimestamp() is only supported on 5.3... But it was released in 2009 :)
– luttkens
Sep 28 '13 at 18:36
1
If you print$paymentDate->getTimestamp() and $contractDateEnd->getTimestamp()
when $contractDateEnd is same as current date it will produce wrong result. example today is 01/05/2015 but if you change $contractDateEnd = new DateTime('2015-05-01'); then it will produce wrong timestamp. Instead of just$paymentDate = new DateTime();
you should use$paymentDate = new DateTime('2015-05-01')
ie. pass current date to it
– RN Kushwaha
May 1 '15 at 10:01
|
show 2 more comments
If hours matter:
$paymentDate = strtotime(date("Y-m-d H:i:s"));
$contractDateBegin = strtotime("2014-01-22 12:42:00");
$contractDateEnd = strtotime("2014-01-22 12:50:00");
if($paymentDate > $contractDateBegin && $paymentDate < $contractDateEnd)
echo "is between";
else
echo "NO GO!";
add a comment |
You are comparing the dates as strings, which won't work because the comparison is lexicographical. It's the same issue as when sorting a text file, where a line 20
would appear after a line 100
because the contents are not treated as numbers but as sequences of ASCII codes. In addition, the dates created are all wrong because you are using a string format string where a timestamp is expected (second argument).
Instead of this you should be comparing timestamps of DateTime
objects, for instance:
$paymentDate = date_create();
$contractDateBegin = date_create_from_format('d/m/Y', '01/01/2001');
$contractDateEnd = date_create_from_format('d/m/Y', '01/01/2015');
Your existing conditions will then work correctly.
I must not be using php5? I got this error: Call to undefined function date_create_from_format()
– Papa De Beau
Sep 28 '13 at 18:14
Try justdate_create
– Ofir Baruch
Sep 28 '13 at 18:15
2
@PapaDeBeau:date_create_from_format
was introduced in PHP 5.3, which has already reached end of life -- yet you are using an even earlier version. I suggest upgrading today if possible.
– Jon
Sep 28 '13 at 18:16
@OfirBaruch: Won't work becaused/m/Y
is not one of the supported formats.
– Jon
Sep 28 '13 at 18:17
1
@borayeris thanks for the catch, fixed.
– Jon
Dec 29 '17 at 9:39
|
show 8 more comments
Based on luttken's answer. Thought I'd add my twist :)
function dateIsInBetween(DateTime $from, DateTime $to, DateTime $subject)
return $subject->getTimestamp() > $from->getTimestamp() && $subject->getTimestamp() < $to->getTimestamp() ? true : false;
$paymentDate = new DateTime('now');
$contractDateBegin = new DateTime('01/01/2001');
$contractDateEnd = new DateTime('01/01/2016');
echo dateIsInBetween($contractDateBegin, $contractDateEnd, $paymentDate) ? "is between" : "NO GO!";
add a comment |
Another solution would have been to consider date written as Ymd.
Written in this "format" this is much easy to compare dates.
$paymentDate = date('Ymd'); // on 4th may 2016, would have been 20160504
$contractBegin = 20010101;
$contractEnd = 20160101;
echo ($paymentDate >= $contractBegin && $paymentDate <= $contractEnd) ? "Between" : "Not Between";
It will always work for every day of the year and do not depends on any function or conversion (PHP will consider the int value of $paymentDate
to compare with the int value of contractBegin
and contractEnd
).
add a comment |
Use directly
$paymentDate = strtotime(date("d-m-Y"));
$contractDateBegin = strtotime("01-01-2001");
$contractDateEnd = strtotime("01-01-2015");
Then comparison will be ok cause your 01-01-2015 is valid for PHP's 32bit date-range, stated in strtotime's manual.
add a comment |
If you need bracket dates to be dynamic ..
$todayStr = date('Y-m-d');
$todayObj=date('Y-m-d', strtotime($todayStr));
$currentYrStr = date('Y');
$DateBegin = date('Y-m-d', strtotime("06/01/$currentYrStr"));
$DateEnd = date('Y-m-d', strtotime("10/01/$currentYrStr"));
if (($todayObj > $contractDateBegin) && ($paymentDate < $contractDateEnd))
$period="summer";
else
$period="winter";
add a comment |
An other solution (with the assumption you know your date formats are always YYYY/MM/DD with lead zeros) is the max() and min() function. I figure this is okay given all the other answers assume the yyyy-mm-dd format too and it's the common naming convention for folders in file systems if ever you wanted to make sure they sorted in date order.
As others have said, given the order of the numbers you can compare the strings, no need for strtotime() function.
Examples:
$biggest = max("2018/10/01","2018/10/02");
The advantage being you can stick more dates in there instead of just comparing two.
$biggest = max("2018/04/10","2019/12/02","2016/03/20");
To work out if a date is in between two dates you could compare
the result of min() and max()
$startDate="2018/04/10";
$endDate="2018/07/24";
$check="2018/05/03";
if(max($startDate,$check)==min($endDate,$check))
// It's in the middle
It wouldn't work with any other date format, but for that one it does. No need to convert to seconds and no need for date functions.
add a comment |
The above methods are useful but they are not full-proof because it will give error if time is between 12:00 AM/PM and 01:00 AM/PM . It will return the "No Go" in-spite of being in between the timing .
Here is the code for the same:
$time = '2019-03-27 12:00 PM';
$date_one = $time;
$date_one = strtotime($date_one);
$date_one = strtotime("+60 minutes", $date_one);
$date_one = date('Y-m-d h:i A', $date_one);
$date_ten = strtotime($time);
$date_ten = strtotime("-12 minutes", $date_ten);
$date_ten = date('Y-m-d h:i A', $date_ten);
$paymentDate='2019-03-27 12:45 AM';
$contractDateBegin = date('Y-m-d h:i A', strtotime($date_ten));
$contractDateEnd = date('Y-m-d h:i A', strtotime($date_one));
echo $paymentDate;
echo "---------------";
echo $contractDateBegin;
echo "---------------";
echo $contractDateEnd;
echo "---------------";
$contractDateEnd='2019-03-27 01:45 AM';
if($paymentDate > $contractDateBegin && $paymentDate < $contractDateEnd)
echo "is between";
else
echo "NO GO!";
Here you will get output "NO Go"
because 12:45 > 01:45
.
To get proper output date in between, make sure that for "AM"
values from 01:00 AM
to 12:00 AM
will get converted to the 24-hour format. This little trick helped me.
add a comment |
Simple solution:
function betweenDates($cmpDate,$startDate,$endDate)
return (date($cmpDate) > date($startDate)) && (date($cmpDate) < date($endDate));
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f19070116%2fphp-check-if-date-between-two-dates%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
11 Answers
11
active
oldest
votes
11 Answers
11
active
oldest
votes
active
oldest
votes
active
oldest
votes
Edit: use <= or >= for count todays date
This is the right answer for your code.. Just use strtotime() php function
$paymentDate = date('Y-m-d');
$paymentDate=date('Y-m-d', strtotime($paymentDate));
//echo $paymentDate; // echos today!
$contractDateBegin = date('Y-m-d', strtotime("01/01/2001"));
$contractDateEnd = date('Y-m-d', strtotime("01/01/2012"));
if (($paymentDate >= $contractDateBegin) && ($paymentDate <= $contractDateEnd))
echo "is between";
else
echo "NO GO!";
Thank you good sir! Maybe it might be good to remove your answer that you added at the top and just leave the one that works for me. Not sure.
– Papa De Beau
Sep 28 '13 at 18:35
4
@PapaDeBeau: You have to keep in mind that those01/01/2001
dates in this solution are inm/d/Y
format, notd/m/Y
.
– Jon
Sep 28 '13 at 20:43
5
I would not use stric comparison, so this is the best $paymentDate >= $contractDateBegin and $paymentDate <= $contractDateEnd
– pollux1er
Aug 14 '14 at 8:39
if (($paymentDate > $contractDateBegin) && ($paymentDate < $contractDateEnd)) can be if (($paymentDate >= $contractDateBegin) && ($paymentDate <= $contractDateEnd)) So that it can check greater than equal to and less than equal to.
– Kamesh Jungi
Jul 15 '15 at 11:18
1
This solution, and every other solution on this page fails when you provide a date that starts before the unix epoch. Try it with 1942/07/28 and see what happens.
– Alex Barker
Jan 4 '16 at 20:53
|
show 3 more comments
Edit: use <= or >= for count todays date
This is the right answer for your code.. Just use strtotime() php function
$paymentDate = date('Y-m-d');
$paymentDate=date('Y-m-d', strtotime($paymentDate));
//echo $paymentDate; // echos today!
$contractDateBegin = date('Y-m-d', strtotime("01/01/2001"));
$contractDateEnd = date('Y-m-d', strtotime("01/01/2012"));
if (($paymentDate >= $contractDateBegin) && ($paymentDate <= $contractDateEnd))
echo "is between";
else
echo "NO GO!";
Thank you good sir! Maybe it might be good to remove your answer that you added at the top and just leave the one that works for me. Not sure.
– Papa De Beau
Sep 28 '13 at 18:35
4
@PapaDeBeau: You have to keep in mind that those01/01/2001
dates in this solution are inm/d/Y
format, notd/m/Y
.
– Jon
Sep 28 '13 at 20:43
5
I would not use stric comparison, so this is the best $paymentDate >= $contractDateBegin and $paymentDate <= $contractDateEnd
– pollux1er
Aug 14 '14 at 8:39
if (($paymentDate > $contractDateBegin) && ($paymentDate < $contractDateEnd)) can be if (($paymentDate >= $contractDateBegin) && ($paymentDate <= $contractDateEnd)) So that it can check greater than equal to and less than equal to.
– Kamesh Jungi
Jul 15 '15 at 11:18
1
This solution, and every other solution on this page fails when you provide a date that starts before the unix epoch. Try it with 1942/07/28 and see what happens.
– Alex Barker
Jan 4 '16 at 20:53
|
show 3 more comments
Edit: use <= or >= for count todays date
This is the right answer for your code.. Just use strtotime() php function
$paymentDate = date('Y-m-d');
$paymentDate=date('Y-m-d', strtotime($paymentDate));
//echo $paymentDate; // echos today!
$contractDateBegin = date('Y-m-d', strtotime("01/01/2001"));
$contractDateEnd = date('Y-m-d', strtotime("01/01/2012"));
if (($paymentDate >= $contractDateBegin) && ($paymentDate <= $contractDateEnd))
echo "is between";
else
echo "NO GO!";
Edit: use <= or >= for count todays date
This is the right answer for your code.. Just use strtotime() php function
$paymentDate = date('Y-m-d');
$paymentDate=date('Y-m-d', strtotime($paymentDate));
//echo $paymentDate; // echos today!
$contractDateBegin = date('Y-m-d', strtotime("01/01/2001"));
$contractDateEnd = date('Y-m-d', strtotime("01/01/2012"));
if (($paymentDate >= $contractDateBegin) && ($paymentDate <= $contractDateEnd))
echo "is between";
else
echo "NO GO!";
edited Aug 4 at 6:59
TarangP
2,2442 gold badges10 silver badges30 bronze badges
2,2442 gold badges10 silver badges30 bronze badges
answered Sep 28 '13 at 18:30
g.m.ashaduzzamang.m.ashaduzzaman
1,3591 gold badge8 silver badges9 bronze badges
1,3591 gold badge8 silver badges9 bronze badges
Thank you good sir! Maybe it might be good to remove your answer that you added at the top and just leave the one that works for me. Not sure.
– Papa De Beau
Sep 28 '13 at 18:35
4
@PapaDeBeau: You have to keep in mind that those01/01/2001
dates in this solution are inm/d/Y
format, notd/m/Y
.
– Jon
Sep 28 '13 at 20:43
5
I would not use stric comparison, so this is the best $paymentDate >= $contractDateBegin and $paymentDate <= $contractDateEnd
– pollux1er
Aug 14 '14 at 8:39
if (($paymentDate > $contractDateBegin) && ($paymentDate < $contractDateEnd)) can be if (($paymentDate >= $contractDateBegin) && ($paymentDate <= $contractDateEnd)) So that it can check greater than equal to and less than equal to.
– Kamesh Jungi
Jul 15 '15 at 11:18
1
This solution, and every other solution on this page fails when you provide a date that starts before the unix epoch. Try it with 1942/07/28 and see what happens.
– Alex Barker
Jan 4 '16 at 20:53
|
show 3 more comments
Thank you good sir! Maybe it might be good to remove your answer that you added at the top and just leave the one that works for me. Not sure.
– Papa De Beau
Sep 28 '13 at 18:35
4
@PapaDeBeau: You have to keep in mind that those01/01/2001
dates in this solution are inm/d/Y
format, notd/m/Y
.
– Jon
Sep 28 '13 at 20:43
5
I would not use stric comparison, so this is the best $paymentDate >= $contractDateBegin and $paymentDate <= $contractDateEnd
– pollux1er
Aug 14 '14 at 8:39
if (($paymentDate > $contractDateBegin) && ($paymentDate < $contractDateEnd)) can be if (($paymentDate >= $contractDateBegin) && ($paymentDate <= $contractDateEnd)) So that it can check greater than equal to and less than equal to.
– Kamesh Jungi
Jul 15 '15 at 11:18
1
This solution, and every other solution on this page fails when you provide a date that starts before the unix epoch. Try it with 1942/07/28 and see what happens.
– Alex Barker
Jan 4 '16 at 20:53
Thank you good sir! Maybe it might be good to remove your answer that you added at the top and just leave the one that works for me. Not sure.
– Papa De Beau
Sep 28 '13 at 18:35
Thank you good sir! Maybe it might be good to remove your answer that you added at the top and just leave the one that works for me. Not sure.
– Papa De Beau
Sep 28 '13 at 18:35
4
4
@PapaDeBeau: You have to keep in mind that those
01/01/2001
dates in this solution are in m/d/Y
format, not d/m/Y
.– Jon
Sep 28 '13 at 20:43
@PapaDeBeau: You have to keep in mind that those
01/01/2001
dates in this solution are in m/d/Y
format, not d/m/Y
.– Jon
Sep 28 '13 at 20:43
5
5
I would not use stric comparison, so this is the best $paymentDate >= $contractDateBegin and $paymentDate <= $contractDateEnd
– pollux1er
Aug 14 '14 at 8:39
I would not use stric comparison, so this is the best $paymentDate >= $contractDateBegin and $paymentDate <= $contractDateEnd
– pollux1er
Aug 14 '14 at 8:39
if (($paymentDate > $contractDateBegin) && ($paymentDate < $contractDateEnd)) can be if (($paymentDate >= $contractDateBegin) && ($paymentDate <= $contractDateEnd)) So that it can check greater than equal to and less than equal to.
– Kamesh Jungi
Jul 15 '15 at 11:18
if (($paymentDate > $contractDateBegin) && ($paymentDate < $contractDateEnd)) can be if (($paymentDate >= $contractDateBegin) && ($paymentDate <= $contractDateEnd)) So that it can check greater than equal to and less than equal to.
– Kamesh Jungi
Jul 15 '15 at 11:18
1
1
This solution, and every other solution on this page fails when you provide a date that starts before the unix epoch. Try it with 1942/07/28 and see what happens.
– Alex Barker
Jan 4 '16 at 20:53
This solution, and every other solution on this page fails when you provide a date that starts before the unix epoch. Try it with 1942/07/28 and see what happens.
– Alex Barker
Jan 4 '16 at 20:53
|
show 3 more comments
You cannot compare date-strings. It is good habit to use PHP's DateTime
object instead:
$paymentDate = new DateTime(); // Today
echo $paymentDate->format('d/m/Y'); // echos today!
$contractDateBegin = new DateTime('2001-01-01');
$contractDateEnd = new DateTime('2015-01-01');
if (
$paymentDate->getTimestamp() > $contractDateBegin->getTimestamp() &&
$paymentDate->getTimestamp() < $contractDateEnd->getTimestamp())
echo "is between";
else
echo "NO GO!";
Again, it must be a php5 issue. I guess I need to update it with godaddy. I got this error: Fatal error: Call to undefined method DateTime::getTimestamp()
– Papa De Beau
Sep 28 '13 at 18:24
What PHP version are you using?
– luttkens
Sep 28 '13 at 18:25
@PapaDeBeau: It's not available on PHP versions older than 5. What does<?php echo phpversion(); ?>
output?
– Amal Murali
Sep 28 '13 at 18:26
1
Looks like getTimestamp() is only supported on 5.3... But it was released in 2009 :)
– luttkens
Sep 28 '13 at 18:36
1
If you print$paymentDate->getTimestamp() and $contractDateEnd->getTimestamp()
when $contractDateEnd is same as current date it will produce wrong result. example today is 01/05/2015 but if you change $contractDateEnd = new DateTime('2015-05-01'); then it will produce wrong timestamp. Instead of just$paymentDate = new DateTime();
you should use$paymentDate = new DateTime('2015-05-01')
ie. pass current date to it
– RN Kushwaha
May 1 '15 at 10:01
|
show 2 more comments
You cannot compare date-strings. It is good habit to use PHP's DateTime
object instead:
$paymentDate = new DateTime(); // Today
echo $paymentDate->format('d/m/Y'); // echos today!
$contractDateBegin = new DateTime('2001-01-01');
$contractDateEnd = new DateTime('2015-01-01');
if (
$paymentDate->getTimestamp() > $contractDateBegin->getTimestamp() &&
$paymentDate->getTimestamp() < $contractDateEnd->getTimestamp())
echo "is between";
else
echo "NO GO!";
Again, it must be a php5 issue. I guess I need to update it with godaddy. I got this error: Fatal error: Call to undefined method DateTime::getTimestamp()
– Papa De Beau
Sep 28 '13 at 18:24
What PHP version are you using?
– luttkens
Sep 28 '13 at 18:25
@PapaDeBeau: It's not available on PHP versions older than 5. What does<?php echo phpversion(); ?>
output?
– Amal Murali
Sep 28 '13 at 18:26
1
Looks like getTimestamp() is only supported on 5.3... But it was released in 2009 :)
– luttkens
Sep 28 '13 at 18:36
1
If you print$paymentDate->getTimestamp() and $contractDateEnd->getTimestamp()
when $contractDateEnd is same as current date it will produce wrong result. example today is 01/05/2015 but if you change $contractDateEnd = new DateTime('2015-05-01'); then it will produce wrong timestamp. Instead of just$paymentDate = new DateTime();
you should use$paymentDate = new DateTime('2015-05-01')
ie. pass current date to it
– RN Kushwaha
May 1 '15 at 10:01
|
show 2 more comments
You cannot compare date-strings. It is good habit to use PHP's DateTime
object instead:
$paymentDate = new DateTime(); // Today
echo $paymentDate->format('d/m/Y'); // echos today!
$contractDateBegin = new DateTime('2001-01-01');
$contractDateEnd = new DateTime('2015-01-01');
if (
$paymentDate->getTimestamp() > $contractDateBegin->getTimestamp() &&
$paymentDate->getTimestamp() < $contractDateEnd->getTimestamp())
echo "is between";
else
echo "NO GO!";
You cannot compare date-strings. It is good habit to use PHP's DateTime
object instead:
$paymentDate = new DateTime(); // Today
echo $paymentDate->format('d/m/Y'); // echos today!
$contractDateBegin = new DateTime('2001-01-01');
$contractDateEnd = new DateTime('2015-01-01');
if (
$paymentDate->getTimestamp() > $contractDateBegin->getTimestamp() &&
$paymentDate->getTimestamp() < $contractDateEnd->getTimestamp())
echo "is between";
else
echo "NO GO!";
answered Sep 28 '13 at 18:22
luttkensluttkens
1,0497 silver badges16 bronze badges
1,0497 silver badges16 bronze badges
Again, it must be a php5 issue. I guess I need to update it with godaddy. I got this error: Fatal error: Call to undefined method DateTime::getTimestamp()
– Papa De Beau
Sep 28 '13 at 18:24
What PHP version are you using?
– luttkens
Sep 28 '13 at 18:25
@PapaDeBeau: It's not available on PHP versions older than 5. What does<?php echo phpversion(); ?>
output?
– Amal Murali
Sep 28 '13 at 18:26
1
Looks like getTimestamp() is only supported on 5.3... But it was released in 2009 :)
– luttkens
Sep 28 '13 at 18:36
1
If you print$paymentDate->getTimestamp() and $contractDateEnd->getTimestamp()
when $contractDateEnd is same as current date it will produce wrong result. example today is 01/05/2015 but if you change $contractDateEnd = new DateTime('2015-05-01'); then it will produce wrong timestamp. Instead of just$paymentDate = new DateTime();
you should use$paymentDate = new DateTime('2015-05-01')
ie. pass current date to it
– RN Kushwaha
May 1 '15 at 10:01
|
show 2 more comments
Again, it must be a php5 issue. I guess I need to update it with godaddy. I got this error: Fatal error: Call to undefined method DateTime::getTimestamp()
– Papa De Beau
Sep 28 '13 at 18:24
What PHP version are you using?
– luttkens
Sep 28 '13 at 18:25
@PapaDeBeau: It's not available on PHP versions older than 5. What does<?php echo phpversion(); ?>
output?
– Amal Murali
Sep 28 '13 at 18:26
1
Looks like getTimestamp() is only supported on 5.3... But it was released in 2009 :)
– luttkens
Sep 28 '13 at 18:36
1
If you print$paymentDate->getTimestamp() and $contractDateEnd->getTimestamp()
when $contractDateEnd is same as current date it will produce wrong result. example today is 01/05/2015 but if you change $contractDateEnd = new DateTime('2015-05-01'); then it will produce wrong timestamp. Instead of just$paymentDate = new DateTime();
you should use$paymentDate = new DateTime('2015-05-01')
ie. pass current date to it
– RN Kushwaha
May 1 '15 at 10:01
Again, it must be a php5 issue. I guess I need to update it with godaddy. I got this error: Fatal error: Call to undefined method DateTime::getTimestamp()
– Papa De Beau
Sep 28 '13 at 18:24
Again, it must be a php5 issue. I guess I need to update it with godaddy. I got this error: Fatal error: Call to undefined method DateTime::getTimestamp()
– Papa De Beau
Sep 28 '13 at 18:24
What PHP version are you using?
– luttkens
Sep 28 '13 at 18:25
What PHP version are you using?
– luttkens
Sep 28 '13 at 18:25
@PapaDeBeau: It's not available on PHP versions older than 5. What does
<?php echo phpversion(); ?>
output?– Amal Murali
Sep 28 '13 at 18:26
@PapaDeBeau: It's not available on PHP versions older than 5. What does
<?php echo phpversion(); ?>
output?– Amal Murali
Sep 28 '13 at 18:26
1
1
Looks like getTimestamp() is only supported on 5.3... But it was released in 2009 :)
– luttkens
Sep 28 '13 at 18:36
Looks like getTimestamp() is only supported on 5.3... But it was released in 2009 :)
– luttkens
Sep 28 '13 at 18:36
1
1
If you print
$paymentDate->getTimestamp() and $contractDateEnd->getTimestamp()
when $contractDateEnd is same as current date it will produce wrong result. example today is 01/05/2015 but if you change $contractDateEnd = new DateTime('2015-05-01'); then it will produce wrong timestamp. Instead of just $paymentDate = new DateTime();
you should use $paymentDate = new DateTime('2015-05-01')
ie. pass current date to it– RN Kushwaha
May 1 '15 at 10:01
If you print
$paymentDate->getTimestamp() and $contractDateEnd->getTimestamp()
when $contractDateEnd is same as current date it will produce wrong result. example today is 01/05/2015 but if you change $contractDateEnd = new DateTime('2015-05-01'); then it will produce wrong timestamp. Instead of just $paymentDate = new DateTime();
you should use $paymentDate = new DateTime('2015-05-01')
ie. pass current date to it– RN Kushwaha
May 1 '15 at 10:01
|
show 2 more comments
If hours matter:
$paymentDate = strtotime(date("Y-m-d H:i:s"));
$contractDateBegin = strtotime("2014-01-22 12:42:00");
$contractDateEnd = strtotime("2014-01-22 12:50:00");
if($paymentDate > $contractDateBegin && $paymentDate < $contractDateEnd)
echo "is between";
else
echo "NO GO!";
add a comment |
If hours matter:
$paymentDate = strtotime(date("Y-m-d H:i:s"));
$contractDateBegin = strtotime("2014-01-22 12:42:00");
$contractDateEnd = strtotime("2014-01-22 12:50:00");
if($paymentDate > $contractDateBegin && $paymentDate < $contractDateEnd)
echo "is between";
else
echo "NO GO!";
add a comment |
If hours matter:
$paymentDate = strtotime(date("Y-m-d H:i:s"));
$contractDateBegin = strtotime("2014-01-22 12:42:00");
$contractDateEnd = strtotime("2014-01-22 12:50:00");
if($paymentDate > $contractDateBegin && $paymentDate < $contractDateEnd)
echo "is between";
else
echo "NO GO!";
If hours matter:
$paymentDate = strtotime(date("Y-m-d H:i:s"));
$contractDateBegin = strtotime("2014-01-22 12:42:00");
$contractDateEnd = strtotime("2014-01-22 12:50:00");
if($paymentDate > $contractDateBegin && $paymentDate < $contractDateEnd)
echo "is between";
else
echo "NO GO!";
edited Apr 1 '15 at 9:47
Rizier123
53.1k15 gold badges70 silver badges114 bronze badges
53.1k15 gold badges70 silver badges114 bronze badges
answered Jan 22 '14 at 15:06
Cristiano Gois de AraújoCristiano Gois de Araújo
1411 silver badge2 bronze badges
1411 silver badge2 bronze badges
add a comment |
add a comment |
You are comparing the dates as strings, which won't work because the comparison is lexicographical. It's the same issue as when sorting a text file, where a line 20
would appear after a line 100
because the contents are not treated as numbers but as sequences of ASCII codes. In addition, the dates created are all wrong because you are using a string format string where a timestamp is expected (second argument).
Instead of this you should be comparing timestamps of DateTime
objects, for instance:
$paymentDate = date_create();
$contractDateBegin = date_create_from_format('d/m/Y', '01/01/2001');
$contractDateEnd = date_create_from_format('d/m/Y', '01/01/2015');
Your existing conditions will then work correctly.
I must not be using php5? I got this error: Call to undefined function date_create_from_format()
– Papa De Beau
Sep 28 '13 at 18:14
Try justdate_create
– Ofir Baruch
Sep 28 '13 at 18:15
2
@PapaDeBeau:date_create_from_format
was introduced in PHP 5.3, which has already reached end of life -- yet you are using an even earlier version. I suggest upgrading today if possible.
– Jon
Sep 28 '13 at 18:16
@OfirBaruch: Won't work becaused/m/Y
is not one of the supported formats.
– Jon
Sep 28 '13 at 18:17
1
@borayeris thanks for the catch, fixed.
– Jon
Dec 29 '17 at 9:39
|
show 8 more comments
You are comparing the dates as strings, which won't work because the comparison is lexicographical. It's the same issue as when sorting a text file, where a line 20
would appear after a line 100
because the contents are not treated as numbers but as sequences of ASCII codes. In addition, the dates created are all wrong because you are using a string format string where a timestamp is expected (second argument).
Instead of this you should be comparing timestamps of DateTime
objects, for instance:
$paymentDate = date_create();
$contractDateBegin = date_create_from_format('d/m/Y', '01/01/2001');
$contractDateEnd = date_create_from_format('d/m/Y', '01/01/2015');
Your existing conditions will then work correctly.
I must not be using php5? I got this error: Call to undefined function date_create_from_format()
– Papa De Beau
Sep 28 '13 at 18:14
Try justdate_create
– Ofir Baruch
Sep 28 '13 at 18:15
2
@PapaDeBeau:date_create_from_format
was introduced in PHP 5.3, which has already reached end of life -- yet you are using an even earlier version. I suggest upgrading today if possible.
– Jon
Sep 28 '13 at 18:16
@OfirBaruch: Won't work becaused/m/Y
is not one of the supported formats.
– Jon
Sep 28 '13 at 18:17
1
@borayeris thanks for the catch, fixed.
– Jon
Dec 29 '17 at 9:39
|
show 8 more comments
You are comparing the dates as strings, which won't work because the comparison is lexicographical. It's the same issue as when sorting a text file, where a line 20
would appear after a line 100
because the contents are not treated as numbers but as sequences of ASCII codes. In addition, the dates created are all wrong because you are using a string format string where a timestamp is expected (second argument).
Instead of this you should be comparing timestamps of DateTime
objects, for instance:
$paymentDate = date_create();
$contractDateBegin = date_create_from_format('d/m/Y', '01/01/2001');
$contractDateEnd = date_create_from_format('d/m/Y', '01/01/2015');
Your existing conditions will then work correctly.
You are comparing the dates as strings, which won't work because the comparison is lexicographical. It's the same issue as when sorting a text file, where a line 20
would appear after a line 100
because the contents are not treated as numbers but as sequences of ASCII codes. In addition, the dates created are all wrong because you are using a string format string where a timestamp is expected (second argument).
Instead of this you should be comparing timestamps of DateTime
objects, for instance:
$paymentDate = date_create();
$contractDateBegin = date_create_from_format('d/m/Y', '01/01/2001');
$contractDateEnd = date_create_from_format('d/m/Y', '01/01/2015');
Your existing conditions will then work correctly.
edited Dec 29 '17 at 9:39
answered Sep 28 '13 at 18:12
JonJon
357k62 gold badges634 silver badges727 bronze badges
357k62 gold badges634 silver badges727 bronze badges
I must not be using php5? I got this error: Call to undefined function date_create_from_format()
– Papa De Beau
Sep 28 '13 at 18:14
Try justdate_create
– Ofir Baruch
Sep 28 '13 at 18:15
2
@PapaDeBeau:date_create_from_format
was introduced in PHP 5.3, which has already reached end of life -- yet you are using an even earlier version. I suggest upgrading today if possible.
– Jon
Sep 28 '13 at 18:16
@OfirBaruch: Won't work becaused/m/Y
is not one of the supported formats.
– Jon
Sep 28 '13 at 18:17
1
@borayeris thanks for the catch, fixed.
– Jon
Dec 29 '17 at 9:39
|
show 8 more comments
I must not be using php5? I got this error: Call to undefined function date_create_from_format()
– Papa De Beau
Sep 28 '13 at 18:14
Try justdate_create
– Ofir Baruch
Sep 28 '13 at 18:15
2
@PapaDeBeau:date_create_from_format
was introduced in PHP 5.3, which has already reached end of life -- yet you are using an even earlier version. I suggest upgrading today if possible.
– Jon
Sep 28 '13 at 18:16
@OfirBaruch: Won't work becaused/m/Y
is not one of the supported formats.
– Jon
Sep 28 '13 at 18:17
1
@borayeris thanks for the catch, fixed.
– Jon
Dec 29 '17 at 9:39
I must not be using php5? I got this error: Call to undefined function date_create_from_format()
– Papa De Beau
Sep 28 '13 at 18:14
I must not be using php5? I got this error: Call to undefined function date_create_from_format()
– Papa De Beau
Sep 28 '13 at 18:14
Try just
date_create
– Ofir Baruch
Sep 28 '13 at 18:15
Try just
date_create
– Ofir Baruch
Sep 28 '13 at 18:15
2
2
@PapaDeBeau:
date_create_from_format
was introduced in PHP 5.3, which has already reached end of life -- yet you are using an even earlier version. I suggest upgrading today if possible.– Jon
Sep 28 '13 at 18:16
@PapaDeBeau:
date_create_from_format
was introduced in PHP 5.3, which has already reached end of life -- yet you are using an even earlier version. I suggest upgrading today if possible.– Jon
Sep 28 '13 at 18:16
@OfirBaruch: Won't work because
d/m/Y
is not one of the supported formats.– Jon
Sep 28 '13 at 18:17
@OfirBaruch: Won't work because
d/m/Y
is not one of the supported formats.– Jon
Sep 28 '13 at 18:17
1
1
@borayeris thanks for the catch, fixed.
– Jon
Dec 29 '17 at 9:39
@borayeris thanks for the catch, fixed.
– Jon
Dec 29 '17 at 9:39
|
show 8 more comments
Based on luttken's answer. Thought I'd add my twist :)
function dateIsInBetween(DateTime $from, DateTime $to, DateTime $subject)
return $subject->getTimestamp() > $from->getTimestamp() && $subject->getTimestamp() < $to->getTimestamp() ? true : false;
$paymentDate = new DateTime('now');
$contractDateBegin = new DateTime('01/01/2001');
$contractDateEnd = new DateTime('01/01/2016');
echo dateIsInBetween($contractDateBegin, $contractDateEnd, $paymentDate) ? "is between" : "NO GO!";
add a comment |
Based on luttken's answer. Thought I'd add my twist :)
function dateIsInBetween(DateTime $from, DateTime $to, DateTime $subject)
return $subject->getTimestamp() > $from->getTimestamp() && $subject->getTimestamp() < $to->getTimestamp() ? true : false;
$paymentDate = new DateTime('now');
$contractDateBegin = new DateTime('01/01/2001');
$contractDateEnd = new DateTime('01/01/2016');
echo dateIsInBetween($contractDateBegin, $contractDateEnd, $paymentDate) ? "is between" : "NO GO!";
add a comment |
Based on luttken's answer. Thought I'd add my twist :)
function dateIsInBetween(DateTime $from, DateTime $to, DateTime $subject)
return $subject->getTimestamp() > $from->getTimestamp() && $subject->getTimestamp() < $to->getTimestamp() ? true : false;
$paymentDate = new DateTime('now');
$contractDateBegin = new DateTime('01/01/2001');
$contractDateEnd = new DateTime('01/01/2016');
echo dateIsInBetween($contractDateBegin, $contractDateEnd, $paymentDate) ? "is between" : "NO GO!";
Based on luttken's answer. Thought I'd add my twist :)
function dateIsInBetween(DateTime $from, DateTime $to, DateTime $subject)
return $subject->getTimestamp() > $from->getTimestamp() && $subject->getTimestamp() < $to->getTimestamp() ? true : false;
$paymentDate = new DateTime('now');
$contractDateBegin = new DateTime('01/01/2001');
$contractDateEnd = new DateTime('01/01/2016');
echo dateIsInBetween($contractDateBegin, $contractDateEnd, $paymentDate) ? "is between" : "NO GO!";
edited Oct 25 '15 at 18:53
answered Oct 25 '15 at 18:19
Vigintas LabakojisVigintas Labakojis
71611 silver badges18 bronze badges
71611 silver badges18 bronze badges
add a comment |
add a comment |
Another solution would have been to consider date written as Ymd.
Written in this "format" this is much easy to compare dates.
$paymentDate = date('Ymd'); // on 4th may 2016, would have been 20160504
$contractBegin = 20010101;
$contractEnd = 20160101;
echo ($paymentDate >= $contractBegin && $paymentDate <= $contractEnd) ? "Between" : "Not Between";
It will always work for every day of the year and do not depends on any function or conversion (PHP will consider the int value of $paymentDate
to compare with the int value of contractBegin
and contractEnd
).
add a comment |
Another solution would have been to consider date written as Ymd.
Written in this "format" this is much easy to compare dates.
$paymentDate = date('Ymd'); // on 4th may 2016, would have been 20160504
$contractBegin = 20010101;
$contractEnd = 20160101;
echo ($paymentDate >= $contractBegin && $paymentDate <= $contractEnd) ? "Between" : "Not Between";
It will always work for every day of the year and do not depends on any function or conversion (PHP will consider the int value of $paymentDate
to compare with the int value of contractBegin
and contractEnd
).
add a comment |
Another solution would have been to consider date written as Ymd.
Written in this "format" this is much easy to compare dates.
$paymentDate = date('Ymd'); // on 4th may 2016, would have been 20160504
$contractBegin = 20010101;
$contractEnd = 20160101;
echo ($paymentDate >= $contractBegin && $paymentDate <= $contractEnd) ? "Between" : "Not Between";
It will always work for every day of the year and do not depends on any function or conversion (PHP will consider the int value of $paymentDate
to compare with the int value of contractBegin
and contractEnd
).
Another solution would have been to consider date written as Ymd.
Written in this "format" this is much easy to compare dates.
$paymentDate = date('Ymd'); // on 4th may 2016, would have been 20160504
$contractBegin = 20010101;
$contractEnd = 20160101;
echo ($paymentDate >= $contractBegin && $paymentDate <= $contractEnd) ? "Between" : "Not Between";
It will always work for every day of the year and do not depends on any function or conversion (PHP will consider the int value of $paymentDate
to compare with the int value of contractBegin
and contractEnd
).
edited May 10 '16 at 21:30
Richard Erickson
2,2316 gold badges19 silver badges34 bronze badges
2,2316 gold badges19 silver badges34 bronze badges
answered May 10 '16 at 20:23
ChtiSebChtiSeb
412 bronze badges
412 bronze badges
add a comment |
add a comment |
Use directly
$paymentDate = strtotime(date("d-m-Y"));
$contractDateBegin = strtotime("01-01-2001");
$contractDateEnd = strtotime("01-01-2015");
Then comparison will be ok cause your 01-01-2015 is valid for PHP's 32bit date-range, stated in strtotime's manual.
add a comment |
Use directly
$paymentDate = strtotime(date("d-m-Y"));
$contractDateBegin = strtotime("01-01-2001");
$contractDateEnd = strtotime("01-01-2015");
Then comparison will be ok cause your 01-01-2015 is valid for PHP's 32bit date-range, stated in strtotime's manual.
add a comment |
Use directly
$paymentDate = strtotime(date("d-m-Y"));
$contractDateBegin = strtotime("01-01-2001");
$contractDateEnd = strtotime("01-01-2015");
Then comparison will be ok cause your 01-01-2015 is valid for PHP's 32bit date-range, stated in strtotime's manual.
Use directly
$paymentDate = strtotime(date("d-m-Y"));
$contractDateBegin = strtotime("01-01-2001");
$contractDateEnd = strtotime("01-01-2015");
Then comparison will be ok cause your 01-01-2015 is valid for PHP's 32bit date-range, stated in strtotime's manual.
edited Sep 28 '13 at 18:40
answered Sep 28 '13 at 18:24
1000Gbps1000Gbps
4391 gold badge14 silver badges22 bronze badges
4391 gold badge14 silver badges22 bronze badges
add a comment |
add a comment |
If you need bracket dates to be dynamic ..
$todayStr = date('Y-m-d');
$todayObj=date('Y-m-d', strtotime($todayStr));
$currentYrStr = date('Y');
$DateBegin = date('Y-m-d', strtotime("06/01/$currentYrStr"));
$DateEnd = date('Y-m-d', strtotime("10/01/$currentYrStr"));
if (($todayObj > $contractDateBegin) && ($paymentDate < $contractDateEnd))
$period="summer";
else
$period="winter";
add a comment |
If you need bracket dates to be dynamic ..
$todayStr = date('Y-m-d');
$todayObj=date('Y-m-d', strtotime($todayStr));
$currentYrStr = date('Y');
$DateBegin = date('Y-m-d', strtotime("06/01/$currentYrStr"));
$DateEnd = date('Y-m-d', strtotime("10/01/$currentYrStr"));
if (($todayObj > $contractDateBegin) && ($paymentDate < $contractDateEnd))
$period="summer";
else
$period="winter";
add a comment |
If you need bracket dates to be dynamic ..
$todayStr = date('Y-m-d');
$todayObj=date('Y-m-d', strtotime($todayStr));
$currentYrStr = date('Y');
$DateBegin = date('Y-m-d', strtotime("06/01/$currentYrStr"));
$DateEnd = date('Y-m-d', strtotime("10/01/$currentYrStr"));
if (($todayObj > $contractDateBegin) && ($paymentDate < $contractDateEnd))
$period="summer";
else
$period="winter";
If you need bracket dates to be dynamic ..
$todayStr = date('Y-m-d');
$todayObj=date('Y-m-d', strtotime($todayStr));
$currentYrStr = date('Y');
$DateBegin = date('Y-m-d', strtotime("06/01/$currentYrStr"));
$DateEnd = date('Y-m-d', strtotime("10/01/$currentYrStr"));
if (($todayObj > $contractDateBegin) && ($paymentDate < $contractDateEnd))
$period="summer";
else
$period="winter";
answered Sep 12 '18 at 16:07
Jim TurnerJim Turner
11 bronze badge
11 bronze badge
add a comment |
add a comment |
An other solution (with the assumption you know your date formats are always YYYY/MM/DD with lead zeros) is the max() and min() function. I figure this is okay given all the other answers assume the yyyy-mm-dd format too and it's the common naming convention for folders in file systems if ever you wanted to make sure they sorted in date order.
As others have said, given the order of the numbers you can compare the strings, no need for strtotime() function.
Examples:
$biggest = max("2018/10/01","2018/10/02");
The advantage being you can stick more dates in there instead of just comparing two.
$biggest = max("2018/04/10","2019/12/02","2016/03/20");
To work out if a date is in between two dates you could compare
the result of min() and max()
$startDate="2018/04/10";
$endDate="2018/07/24";
$check="2018/05/03";
if(max($startDate,$check)==min($endDate,$check))
// It's in the middle
It wouldn't work with any other date format, but for that one it does. No need to convert to seconds and no need for date functions.
add a comment |
An other solution (with the assumption you know your date formats are always YYYY/MM/DD with lead zeros) is the max() and min() function. I figure this is okay given all the other answers assume the yyyy-mm-dd format too and it's the common naming convention for folders in file systems if ever you wanted to make sure they sorted in date order.
As others have said, given the order of the numbers you can compare the strings, no need for strtotime() function.
Examples:
$biggest = max("2018/10/01","2018/10/02");
The advantage being you can stick more dates in there instead of just comparing two.
$biggest = max("2018/04/10","2019/12/02","2016/03/20");
To work out if a date is in between two dates you could compare
the result of min() and max()
$startDate="2018/04/10";
$endDate="2018/07/24";
$check="2018/05/03";
if(max($startDate,$check)==min($endDate,$check))
// It's in the middle
It wouldn't work with any other date format, but for that one it does. No need to convert to seconds and no need for date functions.
add a comment |
An other solution (with the assumption you know your date formats are always YYYY/MM/DD with lead zeros) is the max() and min() function. I figure this is okay given all the other answers assume the yyyy-mm-dd format too and it's the common naming convention for folders in file systems if ever you wanted to make sure they sorted in date order.
As others have said, given the order of the numbers you can compare the strings, no need for strtotime() function.
Examples:
$biggest = max("2018/10/01","2018/10/02");
The advantage being you can stick more dates in there instead of just comparing two.
$biggest = max("2018/04/10","2019/12/02","2016/03/20");
To work out if a date is in between two dates you could compare
the result of min() and max()
$startDate="2018/04/10";
$endDate="2018/07/24";
$check="2018/05/03";
if(max($startDate,$check)==min($endDate,$check))
// It's in the middle
It wouldn't work with any other date format, but for that one it does. No need to convert to seconds and no need for date functions.
An other solution (with the assumption you know your date formats are always YYYY/MM/DD with lead zeros) is the max() and min() function. I figure this is okay given all the other answers assume the yyyy-mm-dd format too and it's the common naming convention for folders in file systems if ever you wanted to make sure they sorted in date order.
As others have said, given the order of the numbers you can compare the strings, no need for strtotime() function.
Examples:
$biggest = max("2018/10/01","2018/10/02");
The advantage being you can stick more dates in there instead of just comparing two.
$biggest = max("2018/04/10","2019/12/02","2016/03/20");
To work out if a date is in between two dates you could compare
the result of min() and max()
$startDate="2018/04/10";
$endDate="2018/07/24";
$check="2018/05/03";
if(max($startDate,$check)==min($endDate,$check))
// It's in the middle
It wouldn't work with any other date format, but for that one it does. No need to convert to seconds and no need for date functions.
edited Jan 22 at 12:00
answered Jan 21 at 18:09
Adam WhateversonAdam Whateverson
3371 silver badge5 bronze badges
3371 silver badge5 bronze badges
add a comment |
add a comment |
The above methods are useful but they are not full-proof because it will give error if time is between 12:00 AM/PM and 01:00 AM/PM . It will return the "No Go" in-spite of being in between the timing .
Here is the code for the same:
$time = '2019-03-27 12:00 PM';
$date_one = $time;
$date_one = strtotime($date_one);
$date_one = strtotime("+60 minutes", $date_one);
$date_one = date('Y-m-d h:i A', $date_one);
$date_ten = strtotime($time);
$date_ten = strtotime("-12 minutes", $date_ten);
$date_ten = date('Y-m-d h:i A', $date_ten);
$paymentDate='2019-03-27 12:45 AM';
$contractDateBegin = date('Y-m-d h:i A', strtotime($date_ten));
$contractDateEnd = date('Y-m-d h:i A', strtotime($date_one));
echo $paymentDate;
echo "---------------";
echo $contractDateBegin;
echo "---------------";
echo $contractDateEnd;
echo "---------------";
$contractDateEnd='2019-03-27 01:45 AM';
if($paymentDate > $contractDateBegin && $paymentDate < $contractDateEnd)
echo "is between";
else
echo "NO GO!";
Here you will get output "NO Go"
because 12:45 > 01:45
.
To get proper output date in between, make sure that for "AM"
values from 01:00 AM
to 12:00 AM
will get converted to the 24-hour format. This little trick helped me.
add a comment |
The above methods are useful but they are not full-proof because it will give error if time is between 12:00 AM/PM and 01:00 AM/PM . It will return the "No Go" in-spite of being in between the timing .
Here is the code for the same:
$time = '2019-03-27 12:00 PM';
$date_one = $time;
$date_one = strtotime($date_one);
$date_one = strtotime("+60 minutes", $date_one);
$date_one = date('Y-m-d h:i A', $date_one);
$date_ten = strtotime($time);
$date_ten = strtotime("-12 minutes", $date_ten);
$date_ten = date('Y-m-d h:i A', $date_ten);
$paymentDate='2019-03-27 12:45 AM';
$contractDateBegin = date('Y-m-d h:i A', strtotime($date_ten));
$contractDateEnd = date('Y-m-d h:i A', strtotime($date_one));
echo $paymentDate;
echo "---------------";
echo $contractDateBegin;
echo "---------------";
echo $contractDateEnd;
echo "---------------";
$contractDateEnd='2019-03-27 01:45 AM';
if($paymentDate > $contractDateBegin && $paymentDate < $contractDateEnd)
echo "is between";
else
echo "NO GO!";
Here you will get output "NO Go"
because 12:45 > 01:45
.
To get proper output date in between, make sure that for "AM"
values from 01:00 AM
to 12:00 AM
will get converted to the 24-hour format. This little trick helped me.
add a comment |
The above methods are useful but they are not full-proof because it will give error if time is between 12:00 AM/PM and 01:00 AM/PM . It will return the "No Go" in-spite of being in between the timing .
Here is the code for the same:
$time = '2019-03-27 12:00 PM';
$date_one = $time;
$date_one = strtotime($date_one);
$date_one = strtotime("+60 minutes", $date_one);
$date_one = date('Y-m-d h:i A', $date_one);
$date_ten = strtotime($time);
$date_ten = strtotime("-12 minutes", $date_ten);
$date_ten = date('Y-m-d h:i A', $date_ten);
$paymentDate='2019-03-27 12:45 AM';
$contractDateBegin = date('Y-m-d h:i A', strtotime($date_ten));
$contractDateEnd = date('Y-m-d h:i A', strtotime($date_one));
echo $paymentDate;
echo "---------------";
echo $contractDateBegin;
echo "---------------";
echo $contractDateEnd;
echo "---------------";
$contractDateEnd='2019-03-27 01:45 AM';
if($paymentDate > $contractDateBegin && $paymentDate < $contractDateEnd)
echo "is between";
else
echo "NO GO!";
Here you will get output "NO Go"
because 12:45 > 01:45
.
To get proper output date in between, make sure that for "AM"
values from 01:00 AM
to 12:00 AM
will get converted to the 24-hour format. This little trick helped me.
The above methods are useful but they are not full-proof because it will give error if time is between 12:00 AM/PM and 01:00 AM/PM . It will return the "No Go" in-spite of being in between the timing .
Here is the code for the same:
$time = '2019-03-27 12:00 PM';
$date_one = $time;
$date_one = strtotime($date_one);
$date_one = strtotime("+60 minutes", $date_one);
$date_one = date('Y-m-d h:i A', $date_one);
$date_ten = strtotime($time);
$date_ten = strtotime("-12 minutes", $date_ten);
$date_ten = date('Y-m-d h:i A', $date_ten);
$paymentDate='2019-03-27 12:45 AM';
$contractDateBegin = date('Y-m-d h:i A', strtotime($date_ten));
$contractDateEnd = date('Y-m-d h:i A', strtotime($date_one));
echo $paymentDate;
echo "---------------";
echo $contractDateBegin;
echo "---------------";
echo $contractDateEnd;
echo "---------------";
$contractDateEnd='2019-03-27 01:45 AM';
if($paymentDate > $contractDateBegin && $paymentDate < $contractDateEnd)
echo "is between";
else
echo "NO GO!";
Here you will get output "NO Go"
because 12:45 > 01:45
.
To get proper output date in between, make sure that for "AM"
values from 01:00 AM
to 12:00 AM
will get converted to the 24-hour format. This little trick helped me.
edited Mar 27 at 5:55
wscourge
5,3468 gold badges29 silver badges53 bronze badges
5,3468 gold badges29 silver badges53 bronze badges
answered Mar 27 at 5:15
pohankar gaurangpohankar gaurang
83 bronze badges
83 bronze badges
add a comment |
add a comment |
Simple solution:
function betweenDates($cmpDate,$startDate,$endDate)
return (date($cmpDate) > date($startDate)) && (date($cmpDate) < date($endDate));
add a comment |
Simple solution:
function betweenDates($cmpDate,$startDate,$endDate)
return (date($cmpDate) > date($startDate)) && (date($cmpDate) < date($endDate));
add a comment |
Simple solution:
function betweenDates($cmpDate,$startDate,$endDate)
return (date($cmpDate) > date($startDate)) && (date($cmpDate) < date($endDate));
Simple solution:
function betweenDates($cmpDate,$startDate,$endDate)
return (date($cmpDate) > date($startDate)) && (date($cmpDate) < date($endDate));
answered Apr 1 at 9:28
SpockSpock
287 bronze badges
287 bronze badges
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f19070116%2fphp-check-if-date-between-two-dates%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
if youre on php5, check out datetime.diff on no2.php.net/manual/en/datetime.diff.php
– Tom
Sep 28 '13 at 18:12
Do you really mean to use date() or strtotime()?
– Mark Baker
Sep 28 '13 at 18:12
I used strtotime and still got a "No Go!";
– Papa De Beau
Sep 28 '13 at 18:15
Then you should read the formats that strtotime() accepts.... but date() is definitely the wrong function to use
– Mark Baker
Sep 28 '13 at 18:17
Why a -1? This seems to be a good question. I placed my code and it has got some good comments. I think this could be helpful to someone else.
– Papa De Beau
Sep 28 '13 at 18:21