PHP - Compare multiple date-time's with +60sec - php

Obviously I'm missing something, but can't figure out how to compare strings which are date&time but both got in different ways.
Code:
$mod_datetime = get_the_modified_date() .' '.get_the_modified_time();
$curr_datetime = date('Y-m-d H:i');
$plus_datetime = strtotime($mod_datetime) <= 0*0*0*60;
date_default_timezone_set('Europe/Warsaw');
if( ($curr_datetime < strtotime($mod_datetime)) > $plus_datetime){
echo 'True';
} elseif ( 0*0*0*60 < (time() < strtotime($mod_datetime)) ) {
echo 'Recently false';
} else {
echo 'False at all';
}
}
In wordpress to get post last modification date and time - has to use $mod_datetime which gives me return like: 2021-05-09 15:21. String $curr_datetime returns me in same format, like: 2021-05-09 15:21 (it's all good with getting last modification date and current date.
But trouble is I'm getting wrong result in echo and obviously my if statement is in wrong way written. How to compare like:
if post has been edited 60 or less seconds ago - return true, otherwise - return false and goodbye, have a nice day.
Any thoughts appreciated.

Related

Php dates is not compare properly

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.

Conditional DateDiff

I had visited some of the post retrieving the date difference between 2 dates in SO but it doesn't gave me the answer I was seeking. Same for reading the documentation, I had problem understanding how it works.
I have tried coding it but it doesn't behave like what I was expecting. Here is my code:
<?php
$currentDate = new DateTime();
$createDateJoin = date_create($getDate['date_joined']);
$dateJoin = date_format($createDateJoin, "Y-m-d");
$yearDifference = $currentDate->diff($createDateJoin);
if ($yearDifference->d < 31 && $yearDifference->m = 0 && $yearDifference->y == 0) {
echo $yearDifference->d . " days";
} else if ($yearDifference->m > 3) {
echo $yearDifference->m . " month";
} else if ($yearDifference->y > 1) {
echo $yearDifference->y . " years";
} else {
echo "Not yet assigned";
}
?>
As you can see from my code above, I am trying to do a print when after calculating the difference between the 2 dates, it meets the condition of $yearDifference->.The behavior from the program that I have experienced does not print out the things I want accordingly (E.g Staff working more than 1 year will print out how many years they have work, months for those who just came in and new staff less than a month will print out days).
I would like to know how does ->d/m/y works and how can I actually make use of the d,m and y to draw out the specific date correctly. And I also noticed that when I treat $yearDifference as a String or int, it comes out different result for the conditions. So what should I treat the type to be to manipulate it more easily? Greatly appreciate the help.
You can use this code to get the date different method diff() object returns more values to check you can print_r your object that will print all data member that are returned via diff() method
<?php
echo get_date_diff(strtotime('1990-10-12'),strtotime('2015-10-14'));
function get_date_diff($date,$dateEnd) {
$dStart = new DateTime(date("Y-m-d", $date));
$dEnd = new DateTime(date("Y-m-d", $dateEnd));
$dDiff = $dStart->diff($dEnd);
return($dDiff->y.' years <br>'.$dDiff->m.' months <br>'.$dDiff->d. ' days');
}
?>

PHP: Add months/days/years to user supplied date

***** SOLVED *****
I have a small program where I get a date from a user - (mm/dd/yyy) - then allow them to add x number of days, months, or years. I've been trying to use the 'strtotime' function but I'm not sure if I am understanding it right.
My code is below. Say, for example, $od (original date) was 7/24/2015. The $d, $m, and $y variables are read from the form filled out by the user. These could be 0's or a user-supplied value.
Before spending hours on this one simple task, I thought I'd ask if I should be approaching this in a different fashion and/or using another function.
// Function to determine new date
function newDate($od, $d, $m, $y) {
$newDate = Date($od, strtotime('+ $d days'));
$newDate = Date($od, strtotime('+ $m months'));
$newDate = Date($od, strtotime('+ $y years'));
if ($od != $newDate) {
return $newDate;
}
else if ($od == $newDate ){
return '[no change]';
}
else {
return '[error occurred]';
}
}
EDIT: Trying now with the interval date as suggested. Can you not parse the interval string together? I feel like this should work (results in some 'non-object' error):
$newDate = $od;
$newDate->add(new DateInterval('P'.$m.'M'.$d.'D'.$y.'Y'));
echo $newDate;
if ($od != $newDate) {
return $newDate->format('mm/dd/yyyy');
EDIT: I figured it out - you can't have '0's in the string so the function won't be as clean as I had hoped.
***** SOLVED *****
The right way : DateTime::add -- date_add — Adds an amount of days, months, years, hours, minutes and seconds to a DateTime object
http://php.net/manual/en/datetime.add.php
Using native functions is always fastest, and in your case seems the proper way to go!

Php custom date format and comparison

In my database I have one table in which I keep registered users.
One column is Date of register and I keep this value in my own string format.
For example "[2013-11-30] [19:42:46]"
Then I want to make a check.
If user is 30 days old or more.
The sure thing is that the above code is wrong.
The problem is that if one user registers at 29/01/2015
will not been showing in 30 last days if the current day is 02/02/2015!
//Datetime
$today = date_parse_from_format("[Y-m-d] [H:i:s]", gmdate("[Y-m-d] [H:i:s]"));
$store = date_parse_from_format("[Y-m-d] [H:i:s]", $row["LastSeen"]);
if (
(($store[year] >= $today[year]) && ($store[month] >= $today[month]))
)
{ $date_last = "<font color='green'>".$row["LastSeen"]."</font>"; }
else
{ $date_last = "<font color='red'>".$row["LastSeen"]."</font>"; }
Use date_create_from_format instead of date_parse_from_format. Then you can simply compare the resulting values:
$today = date_create_from_format("[Y-m-d] [H:i:s]", gmdate("[Y-m-d] [H:i:s]"));
$store = date_create_from_format("[Y-m-d] [H:i:s]", $row["LastSeen"]);
if ($store < $today) {
// ...
}
else {
// ...
}

PHP if time is not passed

I have an XML file with a date and time in it on this format: YYYYMMDDHH
It are 4 digit year, 2 digit month, 2 digit day and 2 digit hour.
How is it possible to make an if/else statement based on this variable.
I have tried this code, but is doesnt work for me.
if( strtotime($validtime) < strtotime('now') ) {
echo "display forecast";
}
else{
echo "hide forecast";
}
Where $validtime is off course the timestring.
Thanks!
strtotime() does not natively recognize the YYYYMMDDHH format so you should manually append minutes and seconds like this:
if( strtotime($validtime.'0000') < strtotime('now') ) {
echo "display forecast";
}
else{
echo "hide forecast";
}
Please note that the code above will almost always enter the if() statement because strtotime('now') produces seconds so something like this will likely be suitable:
if( strtotime($validtime.'0000') < strtotime(date('Y-m-d H:00:00')) ) {
The YYYYMMDDHH format is not recognized with strtotimer(), so you will need to reformat the $validtime variable. Something like this should work:
if(strtotime(substr($validtime,4,2)."/".substr($validtime,6,2)."/".substr($validtime,0,4)." ".substr($validtime,8,2).":00") < strtotime('now') ) {
echo "display forecast";
}
else{
echo "hide forecast";
}

Categories