convert any date string to timestamp without timezone - php

I'm getting xml and rss feeds and putting the data into a database. I've run into two different date formats so far...
Wed, 21 Jul 2010 00:28:50 GMT
And
2010-07-20T17:33:19Z
I'm sure there will be more. My postgresql database for the date is timestamp without time zone. Is there an existing function in php or is there a procedure to convert the any date strings to timestamp without time zone (Y-m-d H:i:s)?

Use date with strtotime:
$date = date('Y-m-d H:i:s', strtotime('Wed, 21 Jul 2010 00:28:50 GMT'));
echo $date;
Result:
2010-07-21 05:28:50
.
$date = date('Y-m-d H:i:s', strtotime('2010-07-20T17:33:19Z'));
echo $date;
Result:
2010-07-20 22:33:19

You don't need to convert it at all. PostgreSQL should convert automatically:
postgres=# create table test_tz (f1 timestamp without time zone);
CREATE TABLE
postgres=# insert into test_tz (f1) values ('Wed, 21 Jul 2010 00:28:50 GMT');
INSERT 0 1
postgres=# insert into test_tz (f1) values ('2010-07-20T17:33:19Z');
INSERT 0 1
postgres=# select f1 from test_tz;
f1
---------------------
2010-07-21 00:28:50
2010-07-20 17:33:19

Timestamps are considered to be UTC.
$dt = new DateTime('Wed, 21 Jul 2010 00:28:50 GMT');
echo $dt->format('U'); // 1279672130
is the same timestamp as
$dt = new DateTime('Wed, 21 Jul 2010 02:28:50 CEST');
echo $dt->format('U'); // 1279672130
Note that the U formatting option requires PHP5.3 though. When supplying a timezone identifier in the Date String, the DateTime object recognizes the Timezone, so when you call the following on the GMT DateTime instance
echo $dt->format('Y-m-d H:i:s');
it will return 2010-07-21 00:28:50. You can change a DateTime object's timezone with it's setTimezone() method though.
$dt = new DateTime('Wed, 21 Jul 2010 02:28:50 GMT+2');
$dt->setTimezone(new DateTimeZone('UTC'));
echo $dt->format('Y-m-d H:i:s'); // 2010-07-21 00:28:50
But if you just need the timestamp, it's not needed.

Related

Display reformatted date from MySQL in PHP

I have a field within a MySQL database that has a date format of the following:
Mon, 17 Aug 2015 19:14:22 +0100
I want to display this date using PHP in the following format:
YYYY-MM-DD HH:MM:SS
I have no idea how to pick out the different elements and reformat them.
Can somebody help?
Many thanks,
John
You can try these :-
$date = "Mon, 17 Aug 2015 19:14:22 +0100";
$newDate = date('Y-m-d H:i:s', strtotime($date));
echo $newDate;
You can do it in mysql
SELECT
DATE_FORMAT(test.dateFrom, '%Y-%M-%d %H:%i:%s') as date,
FROM test
Or in php
$date = date('Y-m-d H:i:s', strtotime($datefrommysql) );

How to format a DATETIME for database insertion?

I have a database that people post to and the posts get inserted.
I want to log the date and time in the database too, using the DATETIME column type. How would I do this using the date() function?
From mysql docs
The DATETIME type is used for values that contain both date and time
parts. MySQL retrieves and displays DATETIME values in 'YYYY-MM-DD
HH:MM:SS' format. The supported range is '1000-01-01 00:00:00' to
'9999-12-31 23:59:59'.
So in PHP you just need to use the date function
$datetime = date('Y-m-d H:i:s');
echo datetime; // display example: 2014-01-27 17:21:34
Different Ways to format Date time in php
<?php
#The two example outputs are based on this time
#eg1: 2010-06-19 15:09:35
#eg2: 2010-06-19 08:30:59
echo date("Y-m-d");
#Output eg1: 2010-06-19
#Output eg2: 2010-06-19
echo date("n/j/y");
#Output eg1: 6/19/10
#Output eg2: 6/19/10
echo date("Y-m-d H:i:s");
#Output eg1: 2010-06-19 15:09:35
#Output eg2: 2010-06-19 08:30:59
echo date("l, jS F Y h:i a");
#Output eg1: Saturday, 19th June 2010 03:09 pm
#Output eg2: Saturday, 19th June 2010 08:30 am
echo date("jS M y g:i A");
#Output eg1: 19th Jun 10 3:09 PM
#Output eg2: 19th Jun 10 8:30 AM
echo date("D, j M Y G:i:s");
#Output eg1: Sat, 19 Jun 2010 15:09:35
#Output eg2: Sat, 19 Jun 2010 8:30:59
Credit to nazly
Doing this can address your concern:
date('Y-m-d H:i:s');
You can also set your DATETIME column to have a default CURRENT_TIMESTAMP.
Here:
$my_date = date("Y-m-d H:i:s");
INSERT INTO my_table (date_time) VALUES ('$my_date');
php DateTime
If you want to insert the current time, you don't need PHP date() (and figure out the proper format string) - just use MySQL NOW():
INSERT INTO mytable (insert_time) VALUES (NOW());

Unable to store date and timestamp values in mysql

I have date and timestamp fields in mysql table.
$timeString="Thu Jul 26 22:45:09 +0000 2012";
$time=strtotime($timeString);
$date=date('Y-m-d', $timeString);
When I execute the query it storing the following values:
date 0000-00-00
time 0000-00-00 00:00:00
Can anyone fix my problem.
you're trying to build your date from $timestring (the string) instead of $time(the timestamp )
try:
$timeString="Thu Jul 26 22:45:09 +0000 2012";
$time=strtotime($timeString);
$date=date('Y-m-d', $time);
you should enable php warnings in your development environment.
date would have told you :)
date() expects parameter 2 to be long, string given
$date = new DateTime('Thu Jul 26 22:45:09 +0000 2012');
echo $date->format('Y-m-d H:i:s');

Linux timestamp to PHP

I have the following timestamp:
1342259667654
which when converted with http://www.epochconverter.com/ gives:
Assuming that this timestamp is in milliseconds:
GMT: Sat, 14 Jul 2012 09:54:27 GMT
Your time zone: 14. juli 2012 11:54:27 GMT+2
And that is the correct time, but when using:
echo date("Y-m-d H:i:s", 1342259667654);
I get the following date:
1904-07-24 10:22:47
How can I get with PHP the exact date out of this time stamp?
Your timestamp needs to be divided by 1000:
echo date("Y-m-d H:i:s", 1342259667654/1000);
$timestamp = 1342259667;
$dt = new DateTime("#$timestamp"); // convert UNIX timestamp to PHP DateTime
echo $dt->format('Y-m-d H:i:s');
You can also do it this way.
The value 1342259667654 is actually in miliseconds, while PHP's date() function is unable to handle miliseconds value. Hence the weird output.

Convert data from email header

Does anyone could help me how to convert data from email header?
I have the next date format from email header:
Wed, 28 Apr 2010 21:59:49 -0400
I need to convert them into mysql Date, or timestamp. Thanks!
You should be using DateTime for this, specifically DateTime::createFromFormat():
$str = 'Wed, 28 Apr 2010 21:59:49 -0400';
$date = DateTime::createFromFormat( 'D, d M Y H:i:s O', $str);
Now, you have a Date object in $date, and you can grab the unix timestamp (if that's what you want), or you can format it into a date for MySQL.
echo $date->getTimestamp(); // Outputs: 1272506389
echo $date->format( 'Y-m-d H:i:s'); // For MySQL column, 2010-04-28 21:59:49
You can see it working in the demo.

Categories