Jump to content

Turn a string variable with Date format into a Date Variable


nightkarnation

Recommended Posts

Hey guys, I have the following:

 

$last_free_date = 'Wed Jun 27 20:32:51 GMT-0300 2012';


$One_Day_Secs = '86400';
$server_current_time = date('D M j H:i:s \G\M\TO Y',strtotime('-'. $One_Day_Secs . 'seconds')); //returns a different date but with the same format as $last_free_date

//Below is the problem: $last_free_date is not being properly recognized...so sometimes I get that $last_free_date is less than $server_current_time when its actually not...or vice verza...any ideas??
if ($last_free_date < $server_current_time)
{
//etc...
}

You can't directly compare the two strings. They would need to be in most-to-least significant value order (YYYY-MM-DD hh:mm:ss) to compare them as strings, otherwise you need to strtotime them for comparison. If the values are coming from a database query, you could simply select only the values that meet the criteria of the comparison.

Thanks a lot Pikachu!!

 

The following seems to work correctly:

 

$last_free_date = strtotime($last_free_date);
$server_current_time = strtotime($server_current_time);


if ($last_free_date < $server_current_time)
{...

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.