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";
}
Related
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.
I am new to PHP and would like to compare a specific time with the current time. I have tried different tricks but nothing works for me.
Eg.
$my_time = '2:00';
$active='';
//echo "The time is " . date("h:ia");die;
if (date('h:i') > date('h:i', strtotime($myTime))) {
$active='Dinner';
$dinner=1;
}
else
{
$active='Lunch';
$lunch=1;
}
You have variable definition
$my_time = '2:00'
but you're using
strtotime($myTime))
This might help you
$my_time ="14:08:10";
if (time() >= strtotime($my_time)) {
echo "Dinner";
}else echo "Lunch";
This will compare given time with current time
try this one .
$my_time ="14:08:10";
if (time() >= strtotime($my_time))
{
echo "Dinner";
}
else
{
echo "Lunch";
}
if this doesnt work try to add date in to time.
because adding date will make time to be more specific.
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.
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');
}
?>
So I'm trying to make a message appear on one specific date of the year.
My code now:
<?php
$year = date("Y");
if(checkdate(5, 22, $year) === TRUE) {
echo '<b>something</b>';
}else {
echo '';
}
?>
But the message appears howsoever, no matter the date.
Hope you can help me,
Thanks in advance!
If you don't have to use checkdate, this is an alternative.
<?php
if (date('Y-m-d') == date('Y').'-05-22') {
echo '<b>something</b>';
} else {
echo '';
}
?>
According to the documentation checkdate only validates dates, it doesn't compare them against the current date.
Returns TRUE if the date given is valid; otherwise returns FALSE.
Use something similar to the code you already have to check the date
if( date("n j") == "5 22")
{
echo '<b>something</b>';
}