comparison of datetime value and system date time fails - php

i have the expiry date saved for each post using date time picker, now i need to check it against the current date and time each time aperson visits the page and display msg according to whether its expired ornot...but the comparison fails
i tried with date(),date create from format and DateTime::createFromFormat...but it fails the check if (is_a($expirydate, 'DateTime')).
$todaydate=new DateTime();
$todaydate->format('d/m/Y H:i:s');
$expirydate=strtotime(get_field('expirydate',$post->ID));
$expirydate=date('d/m/Y H:i:s', $expirydate);
if (is_a($todaydate, 'DateTime')) {
echo "today date is datetime";
if (is_a($expirydate, 'DateTime')) {
echo "expiry date check passed";
}}
if ($expirydate >= $todaydate)
{
echo "not expired";
}
it echos the msg today date is datetime but thats all...doesnt display msg "expiry date check passed" or "not expired"...its shud diplay all of the 3 messages.

Work with strtotime is Old School. Better use DateTime. DateTime objects can also be compared directly.
$expirydate=date_create(get_field('expirydate',$post->ID));
//$expirydate=date_create('today 11:19'); //for a test
$todaydate=new DateTime('Now'); //with current Time
//$todaydate=new DateTime('Today'); //with Time 00:00
if($todaydate > $expirydate) {
echo 'expired';
}

Related

Compare two date time to check whether date is active PHP

I have one datetime in database which is saved in datetime format such as
2019-09-23 06:00:00
now i want to compare whether this datetime is greater than current datetime so as to check whether this record is of past or active for that i am doing something like this:
$currentDate = date('Y-m-d H:i:s');
$scheduleDate=date("Y-m-d H:i:s", strtotime($tableData->start));
if($scheduleDate < $currentDate)
{
echo "active";
}
else{
echo "Inactive";
}
This is database datetime :
2020-04-03 06:15:00
This is current datetime :
2020-04-03 17:50:00
Now i'm getting active but the my database datetime is older because it is of morning,
try to set your default time zone
eg: date_default_timezone_set("America/New_York");
then use below code
$currentdate1=strtotime($currentDate);
$scheduleDate1=strtotime($scheduleDate);
if($currentdate1 < $scheduleDate1)
{
echo "active";
}else
{
echo "Inactive";
}

Check if a timezone has passed a certain time

I am having a bit of trouble working out how to validate whether a timezone has passed a certain time (local to the time zone).
So for instance, if the time in London has passed 18:00:00
$tz = new DateTimeZone('Europe/London');
$datetime1->setTimezone($tz); // calculates with new TZ now
if ($datetime1->format('H:i:s') >= strtotime('18:00:00')) {
echo "time has passed";
} else {
echo "time has NOT passed";
}
The problem with this is that strtotime('18:00:00') seems to be using the server time.
If I echo strtotime('18:00:00'); will return 1470247200 which is the amount of seconds since 1970 but this will not be the 6pm time for another timezone for instance America/New_York which at the time of writing this has not passed.
Any idea how this can be done?
Thanks,
Use DateTime's own comparison feature since it includes time zone support:
$tz = new DateTimeZone('Europe/London');
$datetime1->setTimezone($tz); // calculates with new TZ now
$datetime2 = new \DateTime('18:00:00', $tz);
if ($datetime1 >= sdatetime2) {
echo "time has passed";
} else {
echo "time has NOT passed";
}
I think this should work:
if ($datetime1->format('H:i:s') >= '18:00:00') {
The left side is a string, and every component contains leading zeros, so you can just do a string comparison with the right side.
(This assumes that you consider midnight of the next day to not have "passed" 18:00:00.)

Comparing a Date in 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

How to determine if date entered by the user is in future?

How do I know in PHP that the date entered by the user in future than the current date.
i.e
if($current_date<$future_date)
{
//do all the success actions here
}
else
{
//show the user that they have selected a date in the past.
}
Well you first need to convert the date string using strtotime() function and then check if future date value is greater than current date value.
Here is the code snippet:
$today_date=date('Y-m-d H:i:s');
$current_date=strtotime($today_date);
$future_date=strtotime($future_date);//retrieved from user's input
Now you can call your function:
if($current_date<$future_date)
{
//do all the success actions here
}
else
{
//show the user that they have selected a date in the past.
}
I suppose you know the date format in which your users enters their date cos it will help you understand what date format you will be working with. But irrespective of whatever date format you are working with, you can always convert it to a more suitable one using:
strtotime()
Below is an example of checking a future date (working strictly with date)
$currentDate = date("Y-m-d");
$userFututeDate = "2014-05-08";
if($userFututeDate > $currentDate){
echo 'You have selected a date in the future';
}
else{
echo 'You have selected a date in the past.';
}
Below is another example of checking a future date (working with date and time)
$currentDateTime = date("Y-m-d H:i:s");
$userFututeDateTime = "2014-05-07 05:05:08";
if($userFututeDateTime > $currentDateTime){
echo 'You have selected a date in the future';
}
else{
echo 'You have selected a date in the past.';
}
Perhaps, you need to understand how to play around with strtotime(), find some sample below:
$dateVar = "25-07-2014 05:05:08";
echo date("F j Y", strtotime($dateVar));
echo '<br><br>';
echo date("Y-m-d", strtotime($dateVar));
echo '<br><br>';
echo date("F jS Y, g:i a", strtotime($dateVar));
Check out for more on strtotime()

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

Categories