Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP: Parsing postgre's default string time-stamp format

Tags:

php

postgresql

I have problem to parse the following two time-stamp strings in a generic way?

$timestamp1 = "2013-10-15 19:05:18.756932+03";
$timestamp2 = "2013-10-15 19:05:18.756932+03:00";

I have this so far but it returns with false

$datetime = DateTime::createFromFormat('Y-m-d HH:MM:SS frac "GMT"? [+-] hh ":"? MM?', $timestamp1);
$datetime = DateTime::createFromFormat('Y-m-d HH:MM:SS frac "GMT"? [+-] hh ":"? MM?', $timestamp2);

What's wrong? Thank you.

P.S. I tried this 'Y-m-d HH:MM:SSfrac"GMT"?[+-]hh":"?MM?', this ''Y-m-d HH:MM:SS[0-9]+"GMT"?[+-]hh":"?MM?' and this 'Y-m-d HH:MM:SS [0-9] "GMT"? [+-] hh ":"? MM?' but it always fails.

like image 201
0xC0DEGURU Avatar asked Feb 15 '23 14:02

0xC0DEGURU


2 Answers

Assuming the last part is a timezone offset, Y-m-d H:i:s.ue should work.

If this is posgre's default format you will probably need to handle the case when there are no fractional seconds (2013-10-15 19:05:18+03) with Y-m-d H:i:se.

like image 159
Vatev Avatar answered Feb 17 '23 04:02

Vatev


You have an error in your format string.

$datetime = DateTime::createFromFormat('Y-m-d H:i:s.uP', $timestamp1);

Here is the documentation for the format string options: PHP: DateTime::createFromFormat Manual

like image 45
pogush Avatar answered Feb 17 '23 03:02

pogush