Comparing a Date in PHP - php

I am working on a php app and need some help with comparing a date.
I have a date select input field (datepicker) which thanks to client side code will always post a date in the format: mm/dd/YYYY eg 02/25/2015.
What Iam trying to do is assertain that this date is no later than the current date, using php.
Initially I have set the local timezone with:
date_default_timezone_set('Europe/London');
And within the code I have:
} elseif (date(strtotime($_POST['datepicker'])) > date('m,d,Y')){
$displayblock.= "<br><p>The selected date is in the future!!! </p>".date("d/m/Y", strtotime($_POST['datepicker']));
$alertbox = "<script>alert('".$_POST['datepicker']." is in the future!!! Shall we try that again? :-)');</script>";
This is obviously far from graceful and does not appear to be comaparing dates correctly either.
Can anyone help pls?
Many Thanks,

You can also compare them if they are coming as a string with this function. The first date is your date coming from your html/javascript datepicker, the second date is the actual date of server:
function stringDateIsAPastDate($datestring1){
if ((date("j-m-Y", strtotime($datestring1))) <= (date("j-m-Y"))){
//here the code to do when the date inserted from the website is a past date or today
return(true);
}else{
return(false);
}
}

$now = new DateTime();
$now->format('Y-m-d');
$ding = new DateTime($_POST['datepicker']);
$ding->format('Y-m-d');
if($now < $ding){
echo 'datepicker is after current time';
} else {
echo 'datepicker is before current time';
}

Compare Two Dates
$date1 = new DateTime('May 13th, 1986');
$date2 = new DateTime('October 28th, 1989');
$difference = $date1->diff($date2);
Check this :
http://www.paulund.co.uk/datetime-php
And php.net Manual:
http://php.net/manual/en/class.datetime.php

Related

What's the issue in a code written for comparing the date with today's date?

I'm comparing a date with current date(i.e. today's date). It is expected that the error should come only when the date to be compared is greater than today's date. It should not come for date which is less than or equal to today's date.
I've written following code for it.
$submission_date = $_POST['submission_date']; //The date in mm-dd-yyyy format that is to be tested against today's date. The value in $submission date is 12-25-2014
//This is a future date. Today's date is 12-10-2014 in dd-mm-yyyy format
$current_date = date('m-d-Y');
if (strtotime($submission_date) > strtotime($current_date))
{
echo "Future date not accepted";
}
With the above code I'm not getting errors for future dates, sometimes I'm getting error for previous dates as well.
How to optimize and make this code correct and standard?
If posted format is in m-d-Y, then you cannot convert it to unix timestamp directly with strtotime() function, because it will return false.
If you need to use strtotime() then change the input format to m/d/Y by simple str_replace().
On the other hand, you could use DateTime class, where you can directly compare objects:
$submission_date = DateTime::createFromFormat('!m-d-Y', $submission_date);
$today_date = new DateTime('today');
if ($submission_date > $today_date) {
echo "submission_date is in the future\n";
}
demo
If you need to extract some information from DateTime objects, use format() method on them, which accepts same format as date() function:
echo $today_date->format('m/d/Y'); # 12/11/2014
echo $today_date->format('m-d-Y'); # 12-11-2014
echo $today_date->format('Y-m-d'); # 2014-12-11
echo $today_date->format('Y-Y-Y'); # 2014-2014-2014
demo
I think you need to compare date in 'Y-m-d' or 'd-m-Y' format. I think it is not possible to compare date in 'm-d-Y' format
As we have made changes in your code and we test it works from my side so can you try below.
$submission_date = $_POST['submission_date'];
$current_date = date('d-m-y H:i:s');
if (strtotime($submission_date) > strtotime($current_date))
{
echo "Future date not accepted";
}
Hope this helps.

How to check whether the date coming in $_POST array is not greater than today's date in PHP?

The date to be checked is as follows :
$submission_date = 12-25-2014; //The date in mm-dd-yyyy format that is to be tested against today's date
Now I want to echo the error message since the date contained in a variable $submission_date is a future date.
How should I do this efficiently and effectively using PHP?
Thanks in advance.
Many ways to do this (use DateTime::createFromFormat() to control exact format of input dates, for example) but perhaps the simplest that suits the example is:
$isFuture = (strtotime($submission_date) > strtotime($_POST['current_date']))
Note that OP changed the question. If desired date to test against is not in $_POST array, just replace strtotime($_POST['current_date']) with time() to use current system time.
To compare against current date, disregarding time of day, use:
$today = new DateTime(date("Y-m-d"));
// $today = new DateTime("today"); // better solution courtesy of Glavić
// see http://php.net/manual/en/datetime.formats.relative.php for more info
$today_timestamp = $today->getTimestamp();
If posted format is in m-d-Y, then you cannot convert it to unix timestamp directly with strtotime() function, because it will return false.
If you need to use strtotime() then change the input format to m/d/Y by simple str_replace().
On the other hand, you could use DateTime class, where you can directly compare objects:
$submission_date = DateTime::createFromFormat('!m-d-Y', $submission_date);
$today_date = new DateTime('today');
if ($submission_date > $today_date) {
echo "submission_date is in the future\n";
}
demo
With PHP DateTime you can check whether the input date is future or old w.r.to the todate.
$submission_date = DateTime::createFromFormat('m-d-Y', $submission_date);
$submission_date = $submission_date->format('Y-m-d');
$current_date = new DateTime('today');
$current_date = $current_date->format('Y-m-d');
if ($submission_date > $current_date)
{
echo "Future date";
}
else
{
echo "Old date";
}

Get the date from a time string

Is there a way to convert an input time string (ex: 01:13) to a Zend date object, so that I store it later in a timestamp column in a Mysql database.
Examples:
If the current datetime is 2013-07-15 17:33:07 and the user inputs 18:05 the output should be 2013-07-15 18:05:00.
If the current datetime is 2013-07-15 17:33:07 and the user inputs 02:09 the output should be 2013-07-16 02:09:00. Notice that since the time entered was lower than the current time, so it was treated as tomorrows time.
I simply want to get the next point in time that satisfies the entered time. I'm open for solution using plain PHP or Zend_Date.
I think you should compare the current time with the time entered by the user and create a DateTime object of either "today" or "tomorrow". DateTime accepts strtotime() relative time parameters.
Quick hack. Works as of today, 15.07.2013 23:58 local time:
$nextTime = new DateTime('today 18:10');
if ($nextTime < new DateTime('now')) { // DateTime comparison works since 5.2.2
$nextTime = new DateTime('tomorrow 18:10');
}
echo $nextTime->format('d.m.Y H:i:s');
here is working example for you just add your dynamic variable to check date with user inputs
You can use mktime function to manage your date.
$input_date = date("Y-m-d H:i:s",mktime(18,05,0,date("m"),date("d"),date("Y")));
echo "current time".$current_time = date('Y-m-d H:m:s');
echo "<br>User input is ".$input_date;
if(strtotime($current_time) > strtotime($input_date)){
$input_date = date("Y-m-d H:i:s",mktime(18,05,0,date("m"),date("d")+1,date("Y")));
echo "in";
}else{
// nothing to do
}
echo "<br> result->".$input_date;
i hope it will sure solve your issue

PHP Date Calculation to display the next 1st June

I need to be able to calculate a date using PHP that displays the next 1st of June. So, today is 15th April 2013 therefore I need to display 01/06/2013 (UK format). If the date was 5th August 2013 I would need to display 01/06/2014.
Can anyone help?
Thanks,
John
You can achieve this using :
$now = time();
$june = strtotime("1st June");
if ($now > $june)
echo date("d/m/Y", strtotime('+1 year', $june));
else
echo date("d/m/Y", $june);
Hope this helps :)
For this you can achieve by checking the present month
if(date('m')>06)
{
$date= date('d-m-Y',strtotime("next year June 1st"));
}
else{
$date= date('d-m-Y',strtotime("this year June 1st"));
}
echo $date;
Create a new DateTime object for the current year. DateTime is the preferred way to handle dates in PHP.
If it's too early, create a new datetime object for the following year.
Finally, use 'format' to output.
$d = new DateTime(date('Y').'-08-05');
if ($d < new DateTime()) {
$d = new DateTime((date('Y')+1).'-04-15');
}
echo $d->format('d/m/Y');
You can achieve this using this tutorial. You can define time zone and display the date as per your format.
Check this manual. http://php.net/manual/en/function.date.php
clear examples are given here:
<?php
// set the default timezone to use. Available since PHP 5.1
date_default_timezone_set('UTC');
echo $today = date("d/m/y"); // 03/10/01
?>

Comparing a date to current server date using PHP

I am using the following code to attempt to compare the current date with a date entry in a mySql database. It's code that I have found online and adapted as all the examples I have found hard-code the date to compare the current date with.
The trouble is even dates in the future are being marked as expired and I can't understand why this would be.
I am afraid that I am still new to PHP, so I may be making a schoolboy error!
$exp_date = KT_formatDate($row_issue_whatson1['dateToShow']);
$todays_date = date("d-m-Y");
$today = strtotime($todays_date);
$expiration_date = strtotime($exp_date);
if ($expiration_date > $today) { echo "Not expired"; } else { echo "expired"; }
Any help would be most appreciated.
I should add that the date time format used in the database entries is dd/mm/yyyy
Instead of making a string then converting it to a timestamp, simply use mktime:
<?php
$today = mktime(
0, // hour
0, // minute
0 // seconds
);
?>
The rest of the values will be filled according to today's date. If this still gives problems, put in some echo's for the values of $exp_date and $expiration_date.
Edit
Since this solved the problem, the discrepancy you were seeing was because you were doing the opposite with date('d-m-Y'). You were asking for the current date and the time values are then filled in with the current time. The expiration date in the database is likely set at midnight. With both dates being equal, and it being say 11am now, you are comparing if (00:00:00 > 11:00:00) which fails.
$exp_date = 14/05/2011 // todays date, int
$server_date = server.date() // servers date, int
// check exp_date against server date
if ( $server > $exp_date)
{ echo "Sorry your 'service' has expired"; }
else
{ echo "Welcome 'members_name' to StackOverflow"; }
Try that. However you need the right date format, as server.date() is probably different in PHP.
If problem still persists I would check whether your dates are strings or integers or both. That could possibly be the issue.
Hope that helps.
DL.
Your function does not seem to be valid.
function KT_formatDate( $exp_date){
$exp_date = strtotime($exp_date);
$now = time();
if ($now > $exp_date)
return 'expired';
else
return ' Not expired';
}
$response = KT_formatDate($row_issue_whatson1['dateToShow']);

Categories