0
    <?php


header('Content-Type: application/json; charset=utf-8');
$db = pg_connect("host=localhost port=5432 dbname=postgres user=postgres password=blabla");

$result = pg_query($db,"SELECT kv.ph, kv.date FROM public.kv ORDER BY date DESC LIMIT 5");

$return = array();
while ($row = pg_fetch_array($result)) {

$test = $row[1];
$date = DateTime::createFromFormat('y-m-d', $test);
$formatdate = date('Y, m, d',$date);
    $return[] = array((int)$row[0],"Date($formatdate)");

} 

pg_close($db); 


echo $data=json_encode($return);
?>

I need echo like:
[[6, "Date(2017, 10, 5)"], [9, "Date(2017, 9, 30)"], ...]
I get:
[[6,"Date(1970, 01, 01)"],[9,"Date(1970, 01, 01)"],...]

I am little stuck here, please help. Data type in postgres is date. 2017-10-28

2 Answers 2

1

Error with second parameter in date() function. That must be int but object DateTime given.

Use date_format() instead of date() in $formatdate = date('Y, m, d',$date); row.

So your code can be:

$formatdate = date_format($date, 'Y, m, d');

or

$formatdate = $date->format('Y, m, d');

Sign up to request clarification or add additional context in comments.

1 Comment

$formatdate = date_format($date, 'Y, m, d'); works. thanks
1

1st, DateTime::createFromFormat returns Datetime objext

2nd, i think, from db you recieve year with 4 digits, so you need use format Y

$date = DateTime::createFromFormat('Y-m-d', '2017-10-05');
echo $date->format('Y, m, d'); .. 2017, 10, 05

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.