0
votes

I have a timestamp like 1397105576 and I need to convert it to data format. I used:

echo $today = date('20y-m-d H:m:s',"1397105576");

I am getting:

Severity: Warning
Message: date() expects parameter 2 to be long, object given

in the codeigniter framework.

update: i found the answer

the vaiable should be converted to long ie

echo $today = date('20y-m-d H:m:s',intval("1397105576"));
4
Don't edit answers into your question, post them as a separate answer, In this case, intval is converting the string to an int, not a long; you could just use 1397105576 instead of the circuitous intval("1397105576"), but it shouldn't matter either way -- PHP will deal with the argument being a string. If this really did somehow solve your problem, you can post a separate answer or accept this one which appears to be the same solution - Michael Mrozek
It's NOT "H:m:s", it's "H:i:s". Seems that several answers don't notice this mistake. - erjiang

4 Answers

3
votes

echo date('Y-m-d H:m:s',"1397105576");

Returns

2014-04-10 14:04:56

Update:

That should work in codeigniter also...however there is a CI function that does something similar as above:

$datestring = "Year: %Y Month: %m Day: %d - %h:%i %a";
$time = time();

echo mdate($datestring, $time);

More about this can be found at the user guide. You might be interested in the unix_to_human() function in particular.

2
votes

It seems everywhere except for Russell Dias made the mistake of using m as minutes and not i

Y-m-d H:i:s NOT "Y-m-d H:m:s"

1
votes

your code is working correctly although try it again like this:

<?php
echo $today = date('Y-m-d H:m:s',1397105576);
?>
0
votes

[edit]
I'm going to leave my answer here as a matter of pointing out/teaching you the non-CodeIgnitor sanitized version of PHP's object/typing (+date function!) system, but I HIGHLY suggest using, and accepting, Russell Dias' answer.


Is your example 100% literal?

Is your line of code literally echo $today = date('20y-m-d H:m:s',"1397105576"); or is it something more like echo $today = date('20y-m-d H:m:s',"$date");?

If it's the latter, and the variable is being created by a class, then it may not be properly-typed.

You could probably use… I think an int would suffice here, I think you could do something like echo $today = date('20y-m-d H:m:s',(int)$date);, again, assuming that your line of code is not in-fact literal.

I'm not 100% sure that int would work, if it doesn't, (float) is the only other numeric type that possible could. Perhaps (string)?

See Type Juggling.


As an aside, why are you using 20y? Please change that to a Y

From PHP's Date Docs:
Y | A full numeric representation of a year, 4 digits | Examples: 1999 or 2003
y | A two digit representation of a year | Examples: 99 or 03