I am trying to check whether user submitted date is within range or not.
<?php
$datesyntx = "/^(19|20)\d\d[\-\/.](0[1-9]|1[012])[\-\/.](0[1-9]|[12][0-9]|3[01])$/";
$paydate = "2015-03-01";
if (preg_match($datesyntx, $paydate)) {
if(strtotime($paydate) > strtotime('2015-04-23') && strtotime($paydate) < strtotime('2015-07-23')) {
}
}
?>
It is not working what actually I am trying to get.
Try this code :
$paydate = date('2015-03-01');
$DateBegin = date('2010-01-01');
$DateEnd = date('2016-01-01');
if (($paydate > $DateBegin) && ($paydate < $DateEnd))
{
echo "is between";
}
else
{
echo "not between!";
}
P.S: this question was answered before at stackoverflow:
PHP check if date between two dates
EDIT2: easy way .
No need to use regex in this scenario, use simple DateTime class to check whether user submitted date is within range or not.you can also use typical strtotime() to do this job. But DateTime class will be great fun for this.
<?php
$userSubmitted = new DateTime("2015-04-01");
$startDate=new DateTime("2014-02-01 20:20:00");
$endDate=new DateTime("2015-04-30 23:50:11");
if ($userSubmitted > $startDate && $userSubmitted < $endDate)
{
return true; #date is within the range of yours
}
return false; #date not within the range of yours
Related
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));
}
I am new in PHP
when I am trying to do this
if( date('m-Y',strtotime('2016-11-01 00:00:00')) < date('m-Y') ) {
echo "yes";
} else {
echo 'no';
}
but it always do false [output 'no'].
I must need to compare months is less than current month , means compare date do not have same months
where I am wrong to compare that date ?
Use DateTime to compare dates:
$date = new DateTime('2016-11-01 00:00:00');
$now = new DateTime();
if ($date < $now && $date->format('m-Y') != $now->format('m-Y')) {
echo 'yes';
} else {
echo 'no';
}
I copied your program so that it reads:
<?php
$x=date('m-Y',strtotime('2016-11-01 00:00:00'));
echo "$x\n";
$y=date("m-Y");
echo "$y\n";
if ($x < date('m-Y') ) {
echo "yes";
} else {
echo 'no';
}
On running it the output is:
# php x.php
11-2016
01-2017
no
That is why it fails. If you are checking for just the month you need to check for equality. Otherwise you need to reorder the date formatting to be "Y-m" (not 'm-Y') for less/greater than comparisons. Comparing the strings is fine.
date function always return a string. In your if construct you compare two strings. For current time:
"11-2016" < "01-2017"
In this case "11-2016" greater than "01-2017".
It will be better to use DateTime class.
$date = new DateTime('2016-11-01 00:00:00');
$now = new DateTime();
if ($date < $now && $date->format('m-Y') != $now->format('m-Y')) {
echo 'yes';
} else {
echo 'no';
}
or in your example you need to change format to 'Y-m'.
You should use a decent format to compare the dates. Instead of m-Y, use Y-m-d.
Currently, you are converting the dates to strings, with their months first. So the first date becomes 11-2016, the second becomes 01-2017. PHP compares these as strings, and finds that 0 is less thans 1, so considers the second string to be less.
It's will be right ? for use < , > operator for compare date on php ?
I tested it. And it's work good. But i not founded documents for advice about use < , > operator for compare date on php.
Please tell me can we use < , > operator for compare date on php ?
<?PHP
$today = "2010-10-19";
$expired = "2012-12-10";
if($expired > $today)
{
echo "not expired";
}
else
{
echo "expired";
}
?>
As long as your dates are in a comparable format this will work. Since they are compared as strings you need to make sure that the larger dates components are first and the smaller ones last. For example, the year, then the month, then the date. Always include leading zeros. That's what you do in your example so it works.
If you do not have the dates in this format your comparison will fail some of the time. For example:
'2016-07-04' > '2016-01-07' // true
'04-07-2016' > '07-01-2016' // false
The best way to compare dates is with DateTime() as DateTime objects are comparable and also account for all things date related like leap year and DST.
<?PHP
$today = new DateTime("2010-10-19");
$expired = new DateTime("2012-12-10");
if($expired > $today)
{
echo "not expired";
}
else
{
echo "expired";
}
Use strtotime to convert datetime description into a Unix timestamp and you can easily compare timestamp with < or > operator
$today = strtotime('2010-10-19');// return 1287426600
$expired = strtotime('2012-12-10');// return 1355077800
if ($expired > $today) {// easily comparison of 1355077800 > 1287426600
echo "not expired";
} else {
echo "expired";
}
I've looked at many posts on here and I still cant figure this out.
I am trying to verify that someone is older than 13 before they register for my site. This is what i have so far
<?php
if (is_string($_POST['birthday']) && $_POST['birthday'] != 'mm/dd/yyyy')
{
$dateObj = new DateTime($_POST['birthday']);
$ageLimit = new DateTime('13 years');
$now = new DateTime(date("Y/m/d"));
$checkDate = $now->diff($ageLimit);;
if($checkDate > $dateObj)
{
$errors[]='You must be atleast 13 years old to join.';
}
else
{
$bday = mysqli_real_escape_string($db,$_POST['birthday']);
}
}
else
{
$errors[]= 'Enter your birthday.';
}
The code will always run to
$bday = mysqli_real_escape_string($db,$_POST['birthday']);}
no matter what is entered in the date field and the outcome is always 1.
Can anyone help me with this? I cant figure this one out on my own.
<b>Birth Date</b><br><input type="date" name="birthday"
value=<?php if(isset($_POST['birthday']))echo $_POST['birthday'];?>><br>
Comparaison operators work with DateTime, see the answer here.
So something like this should work
$dateObj=new DateTime($_POST['birthday']);
$ageLimit=new DateTime('-13 years');
if($dateObj > $ageLimit){
//TOO YOUNG
}
EDIT per comment
Replace
if(isset($_POST['birthday']))echo $_POST['birthday'];
with
if(isset($_POST['birthday'])) {
echo $_POST['birthday'];
} else {
echo 'mm/dd/yyyy';
}
Or change
if (is_string($_POST['birthday']) && $_POST['birthday'] != 'mm/dd/yyyy')
To
if (!empty($_POST['birthday']) && is_string($_POST['birthday']))
You have several errors
'13 years' is not a valid value for DateTime()
A date in the 'Y/m/d' format is not a valid format for DateTime()
$checkDate is a DateInterval object and is not comparable to a DateTime object
You can fix this and simplify your code by comparing DateTime objects which are comparable:
$birthday = new DateTime($_POST['birthday']);
$ageLimit = new DateTime('-13 years');
if ($birthday < $ageLimit) {
// they're old enough
}
else {
// too young
}
Demo
It might be easier to use strtotime to calculate the date difference. The higher the number the younger somebody is. So if the persons age is higher than the minimal age number they are not old enough.
if(is_string($_POST['birthday'])&&$_POST['birthday']!='mm/dd/yyyy') {
$minAge = strtotime("-13 years");
$dateObject = strtotime($_POST['birthday']);
if($dateObject > $minAge) {
$errors[]= 'You must be atleast 13 years old to join.';
}
} else {
$errors[]='Enter your birthday.';
}
I have a database that has a few unknown months or days formatted like so: 1999-01-00 / 1999-00-00.
Now, I've been using DateTime() to format the days that are full, which works fine.
I'd like to have abbreviations for unknown days, but known months using $DateTime->format('M Y'); which outputs Sep 1999
and
$DateTime->format('Y'); for 1999 that is for unknown months and days.
DateTime() Doesn't allow 00 values for months/days, so what are some ways to get around this?
Just what I was looking for (and didn't want to hear).
Here's a basic php function for those of you who are lazy like me.
function formatDatabaseDate($date, $delimiter = '/')
{
// this function works only on mysql date fields,
// and takes into account partial dates.
if ($date != '' && $date != NULL)
{
$datePieces = explode('-', $date);
if (count($datePieces) == 3 &&
strlen($datePieces[0]) == 4 &&
strlen($datePieces[1]) == 2 &&
strlen($datePieces[2]) == 2)
{
$datestring = '';
$months['01'] = 'Jan';
$months['02'] = 'Feb';
$months['03'] = 'Mar';
$months['04'] = 'Apr';
$months['05'] = 'May';
$months['06'] = 'Jun';
$months['07'] = 'Jul';
$months['08'] = 'Aug';
$months['09'] = 'Sep';
$months['10'] = 'Oct';
$months['11'] = 'Nov';
$months['12'] = 'Dec';
if ($datePieces[2] != '00' && $datePieces[1] != '00')
{
$datestring = $datePieces[2] . $delimiter
. $months[$datePieces[1]] . $delimiter
. $datePieces[0];
}
else if ($datePieces[1] != '00')
{
$datestring = $months[$datePieces[1]] . $delimiter
. $datePieces[0];
}
else
{
$datestring = $datePieces[0];
}
return $datestring;
}
else
{
trigger_error('date is not in a valid mysql format');
return false;
}
}
else
{
trigger_error('empty date passed to format command');
return false;
}
}
Unfortunately, the DateTime class is pretty much a wrapper for a UNIX timestamp. (I said pretty much, I know there's more to it than just that.) The way that it works with dates were one of the elements is zero is simply to calculate it as a synonym for the previous period. (e.g.: April 0 is calculated to become March 31.)
Really, you just have to bypass PHP's built in date functionality and write your own. The good news is that you can indeed do this. MySQL will actually return values as simple strings, and, when it comes to dates, the natural response that the actual C libraries use is to return the date pretty much as an ISO 8601 string. (There's that phrase pretty much again.)
If I were writing this, I would read the value raw with mysql_fetch_row (or your own method) and check if all the elements (i.e.: year, month, day) are defined. If they are, use the DateTime method. If not, I would write a piece of code to pull apart the elements that are defined and format it accordingly.