PHP - Check date is in the past but exclude todays date - php

I'm using the following function to check whether the date is in the past, but I want to exclude today’s date from it as it returns true for today's date also.
For example, if I pass today's date 2018-03-15, it returns true when it shouldn't.
function is_date_in_past($date):bool
{
return !empty($date) && (strtotime($date) < time());
}
Can anybody please help me with this, that how to exclude today's date from being recognised as past date?
Thanks in advance.

Use DateTime objects. They are easier to use than the old date & time functions and the code is more clear.
function is_date_in_past($date) {
return new DateTime($date) < new DateTime("today");
}

If you'd like to use old date/time functions, you can do like that:
function is_date_in_past(string $date) :bool
{
return !empty($date) && strtotime($date) < strtotime(date('Y-m-d'));
}
but better to consider to use https://stackoverflow.com/a/49305230/9254020 variant

Related

Storing time strings to TIME column in Laravel

I use a timepicker to get a time string(eg: '1:00am' or '12:00pm'). I need to modify and store this in a mysql TIME data type column as eg: '12:00:00'. Now, since the TIME column stores in 24 hrs format, I need to "modify" it. I can't seem to find any help to modify using Carbon, since I'm not using any dates. I'm using a mutator in Laravel, but would like to know if there is a better way like using Carbon for dates.
public function setTimeAttribute($time) {
if(substr($time, -2, 0) == 'am') {
// do nothing
} else if(substr($time, -2, 0) == 'pm') {
// increase hours.ie: 1:00pm to 13:00:00
}
}
Any help would be appreciated! Thanks.
Use PHP strtotime() function.
$time = "12:00am";
$timestamp = strtotime($time);
echo $timestamp;
//Will print something like: 1469228400
Or any other date format you prefer.
It's best to store time as timestamp, to convert it back you can use date() function.
E.g: $value = date("H:i:s", $timestamp);

php strtotime() values not working as expected

this code keeps telling me that $lasUpdate is always greater than $yesterday no matter the change i make to $yesterday result is (12/31/14 is greater than 01/19/15 no update needed). i feel like i'm missing something simple thank you in advance it is greatly appreciated.
$result['MAX(Date)']='12/31/14';
$lastUpdate = date('m/d/y', strtotime($result['MAX(Date)']));
$yesterday = date('m/d/y', strtotime('-1 day'));
if($lastUpdate<$yesterday){echo $lastUpdate.'is less '.$yesterday.'<br>'.'update needed';}
if($lastUpdate>=$yesterday){echo $lastUpdate.'is greater than '.$yesterday.'<br>'.'no update needed';
You have fallen victim to PHP type juggling with strings. A date function has a return value of a string. You cannot compare dates in their string format since PHP will juggle strings into integers in the context of a comparison. The only exception is if the string is a valid number. In essence, you are doing:
if ('12/31/14' < '01/19/15') { ... }
if ('12/31/14' >= '01/19/15') { ... }
Which PHP type juggles to:
if (12 < 1) { ... }
if (12 >= 1) { ... }
And returns false on the first instance, and true on the second instance.
Your solution is to not wrap date around the strtotime functions, and just use the returned timestamps from the strtotime functions themselves to compare UNIX timestamps directly:
$lastUpdate = strtotime($result['MAX(Date)']);
$yesterday = strtotime('-1 day');
You will however want to use date when you do the echo back to the user so they have a meaningful date string to work with.
Try something like this:
$lastUpdate = strtotime($result['MAX(Date)']);
$yesterday = strtotime('-1 day');
if ($lastUpdate < $yesterday) { /* do Something */ }
12/31/14 is greater than 01/19/15
Because 1 is greater than 0. If you want to compare dates that way you will need to store them in a different format (from most to least significant digit), for example Ymd.
Or store the timestamps you are making in the different variables and compare them.

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";
}

compare date trimming

I have a field (nonTimeStampDate) that has date like this
2010-03-15
and I want to check it against another field (timeStampDate) which is
2010-03-15 15:07:45
to see if the date matchs. But as you can see since the format is different it doesnt match even though the date is same.
Any help will be appreciated.
thanks
My first thought is using date() and strtotime() to reformat them.
$date1 ="2010-03-15";
$date2 = "2010-03-15 15:07:45";
if (date('Y-m-d', strtotime($date1)) == date('Y-m-d', strtotime($date2)))
{
//do something
}
This would work and give you more flexibility in how the two dates formatted to begin with. Not the most elegant.
You might want to try this code:
if (strpos($date1, $date2) !== false) {
// Your code here
}
It is a bit faster than exploding the value by a space as suggested by Anax. Make sure that $date2 contains the shorter of the two dates.
If you are certain about the input string format, you need to split it and take the first part, in order to compare it with your original date:
$splits = explode(' ', $original);
$datapart = $splits[0];
if ($datepart == $nonTimeStampDate) {
// your code here
}
What Anax says, or if these values are in MySQL tables, you can use MySQLs datetime functions (like DATE()) to compare them in MySQL.
Then, just compare the date part:
<?php
if( substr('2010-03-15 15:07:45', 0, 10) == '2010-03-15' ){
echo 'Dates match';
}
?>
Whatever, if you need to do serious date handling, you need to use a proper format, such as a DateTime object.
$firstDate = date("Y-m-d", strtotime("2010-03-15"));
$secondDate = date("Y-m-d", strtotime("2010-03-15 15:07:45"));
if( $firstDate == $secondDate ) {
// true
}

How can I check if the current date/time is past a set date/time?

I'm trying to write a script that will check if the current date/time is past the 05/15/2010 at 4PM
How can I use PHP's date() function to perform this check?
Since PHP >= 5.2.2 you can use the DateTime class as such:
if (new DateTime() > new DateTime("2010-05-15 16:00:00")) {
# current time is greater than 2010-05-15 16:00:00
# in other words, 2010-05-15 16:00:00 has passed
}
The string passed to the DateTime constructor is parsed according to these rules.
Note that it is also possible to use time and strtotime functions. See original answer.
There's also the DateTime class which implements a function for comparison operators.
// $now = new DateTime();
$dtA = new DateTime('05/14/2010 3:00PM');
$dtB = new DateTime('05/14/2010 4:00PM');
if ( $dtA > $dtB ) {
echo 'dtA > dtB';
}
else {
echo 'dtA <= dtB';
}
Check PHP's strtotime-function to convert your set date/time to a timestamp: http://php.net/manual/en/function.strtotime.php
If strtotime can't handle your date/time format correctly ("4:00PM" will probably work but not "at 4PM"), you'll need to use string-functions, e.g. substr to parse/correct your format and retrieve your timestamp through another function, e.g. mktime.
Then compare the resulting timestamp with the current date/time (if ($calulated_timestamp > time()) { /* date in the future */ }) to see whether the set date/time is in the past or the future.
I suggest to read the PHP-doc on date/time-functions and get back here with some of your source-code once you get stuck.
date_default_timezone_set('Asia/Kolkata');
$curDateTime = date("Y-m-d H:i:s");
$myDate = date("Y-m-d H:i:s", strtotime("2018-06-26 16:15:33"));
if($myDate < $curDateTime){
echo "active";exit;
}else{
echo "inactive";exit;
}

Categories