Domanda

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!

È stato utile?

Soluzione

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')) ) {

Altri suggerimenti

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";
}
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top