llal
llal

Reputation: 1

timestamp to date conversion problem in php

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"));

Upvotes: 0

Views: 2631

Answers (4)

Francois Raubenheimer
Francois Raubenheimer

Reputation: 21

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"

Upvotes: 2

Russell Dias
Russell Dias

Reputation: 73382

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.

Upvotes: 3

VxJasonxV
VxJasonxV

Reputation: 975

[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

Upvotes: 0

Vaibhav Gupta
Vaibhav Gupta

Reputation: 1612

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

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

Upvotes: 1

Related Questions