fightstarr20
fightstarr20

Reputation: 12608

PHP date not working with saved value timestamp

I am trying to convert a timestamp using php date like this...

update_option('last_lookup', time());
$last_lookup = get_option('last_lookup');

$timestamp1 = date("c", $last_lookup);
$timestamp2 = date("c" , 1549133279);

echo $last_lookup;
echo $timestamp1;
echo $timestamp2;

Only $timestamp2 is returning the correct result, if I echo $last_lookup then I get the correct timestamp back.

Could it be that $last_lookup is a string and date needs an integer?

Can anyone suggest a fix?

UPDATE

Doing a var_dump on $last_lookup gives me...

string(10) "1549133279"

Upvotes: 0

Views: 112

Answers (2)

PsychoMantis
PsychoMantis

Reputation: 1015

Your timestamp string is a unix timestamp from time()

You can convert it to timestamp format with the date() function.

For example, passing your string of

1549133279

to

$timestamp = date('Y-m-d H:i:s', $t);

yields 2019-02-02 18:47:59

Edit:

Just as an update, I took your code and placed it in the index.php file of wp-content/themes/twentynineteen/index.php (the theme I was using to test) out of curiosity and had zero issue with it. I added a break at the end of each echo and was able to see the following:

1549137644
2019-02-02T20:00:44+00:00
2019-02-02T18:47:59+00:00

Of course, the times are different because one was determined at runtime and the other is absolute so I am fairly certain that there isn't any issue with the code you posted here.

Upvotes: 1

Googlian
Googlian

Reputation: 6743

This method is working fine and tested, try this way and if you still face issue, debug your code with other functions

$last_lookup = time();

$timestamp1 = date("c", 1549133279);
$timestamp2 = date("c", "1549133279");
$timestamp3 = date("c", $last_lookup);

echo $last_lookup;
echo "<br>";
echo $timestamp1;
echo "<br>";
echo $timestamp2;
echo "<br>";
echo $timestamp3;

Upvotes: 0

Related Questions