Php proper time conversion for MySQL - php

Getting time as following in json result from Google API:
2014-08-25T20:02:24.000Z
how to convert and prepare it for MySQL datetime field?

$dateTime=new DateTime('2014-08-25T20:02:24.000Z');
echo $dateTime->format('Y-m-d H:i:s') ;
outputs as
2014-08-25 20:02:24 mysql DATETIME format

This is a ISO 8601 date. You can convert it directly to a DateTime object with:
$date = new DateTime('2014-08-25T20:02:24.000Z');
Or to a timestamp to be used with the classic PHP time functions with strtotime():
$time = strtotime('2014-08-25T20:02:24.000Z');
Once you have that simply convert it to the format MySQL uses, Y-m-d H:i:s, by using one of those:
$to_mysql = $date->format('Y-m-d H:i:s');
$to_mysql = date('Y-m-d H:i:s', $time);

As a simple ISO 8601 representation it is almost what MySQL understands. The only thing that is problematic issue is the "Z" as MySQL does not support time-zones nor does it ignore "Z".
Thus simply touch up the format a bit (such is not a general solution for all dates but it does present a valid solution for this problem, when the date is not used or checked pre-SQL);
$iso = "2014-08-25T20:02:24.000Z"; // literal JSON text
$iso = preg_replace("/Z$/", "", $iso); // -> "2014-08-25T20:02:24.000"
And insert it into MySQL as normal (with placeholders) like a string;
$msqli_stmt->bind_param("s", $iso);
$pdo_stmt->bindValue(":dt", $iso);
Consider this SQL example;
create table x (y datetime)
insert into x (y) values ('2014-08-25T20:02:24.000')
select * from x
--> August, 25 2014 20:02:24+0000

Related

How to format `23/05/2012` into insertable value in DATETIME column?

I have dates formatted as d/m/y. How can I insert them into a DATETIME column?
Use MySQL's STR_TO_DATE() function:
INSERT INTO my_table VALUES (STR_TO_DATE('26/5/12', '%e/%c/%y'))
You need to use php's date() function along with strtotime() to convert date to any format you want.
MySQL database stores the date in YY-MM-DD format for datetime datatype, so if for example you have a date
$date = '26/05/2012';
You can convert it by using date() and strtotime()
$formatDate = date('Y-m-d', strtotime('26/05/2012'));
This will convert the date from 26/05/2012 to 2012-05-26 which then can be inserted into the database.
If you are using a timestamp datatype to store the date in your database, then all you need is to convert the current date into unix timestamp and store in database for example.
$date = strtotime('26/05/2012');
//this will convert the date to unix timestamp
Update:
as pointed out by #wallyk (thank you), strtotime() does not handles dd/mm/yy format. the fix is to replace the slash / by -m below code should work for you.
date('Y-m-d', strtotime(str_replace('/', '-', '26/05/2012')));
Try this:
$mysqldate = date("m/d/y g:i A", $datetime);
$date = date('d/m/Y');
$date = strtotime($date); //in unix time stamp format
Basically american date format is MM/DD/YYYY and you are providing DD/MM/YYYY so thats why startotime() returns you a null values on this input; and i prefer you must follow standard date format of american (MM/DD/YYYY) because if you are using mentioned format of date that will create more problems as well in different places ..
if you check by this
echo date('Y-m-d', strtotime('05/26/2012') );
and it is working fine ..
you could change your DATE column into a String Column and insert the data when ever you want 2 check if the date is right you can use a regular expression to do so

How do I format this time/date in PHP?

When I make projects on my website, I've saved the time and date that I made them on in MySQL as '2012-02-27 12:32:36' format.
How can I get this to display as:
"February 27, 2012 12:32 p.m."?
I've looked at other date formatting on PHP and read on the PHP website, but all of the functions they provide don't seem to be for grabbing a date in the format that I've saved it in.
First, convert the MySQL DateTime string you have into a DateTime object by using DateTime::createFromFormat()
$mysqlDateTime = '2012-02-27 12:32:36';
$date = DateTime::createFromFormat('Y-m-d H:i:s', $mysqlDateTime);
Then, simply call DateTime->format() with the desired format string:
$formattedDate = $date->format('F j, Y g:i a');
You can also use the MySQL UNIX_TIMESTAMP() date function (to extract a unix timestamp compatible with date()) or use strtotime(), however note that these methods do not support dates after January 19, 2038 on 32-bit systems due to overflow (the two methods do not support PHP's automatic number variables promotion).

Date and Time functions in PHP & MySQL

I have date and time stored in my database as MySQL DATETIME datatype. While inserting into the database, I am using the following PHP variable
$serverTime = strftime("%Y-%m-%d %H:%M:%S",$_SERVER['REQUEST_TIME']);
A sample DATETIME stored in database is: 2011-11-26 01:00:27
Now my website needs the date to be displayed in the following format: November 26, 2011 (time is not required)
How can I do it? I am trying the below in PHP but wrong output I am getting.
echo strftime("%B %d, %Y",$serverTime)
The PHP strftime() function wants input to be a 32-bit integer timestamp, which is the number of seconds since 1970-01-01 00:00:00.
The default output format for MySQL datetime is YYYY-MM-DD HH:MM:SS. When you use this string in PHP, the leading digits are converted into the integer 2011, which is interpreted to be within the first hour of 1970-01-01.
So you must fetch the datetime from MySQL in another format. Choices:
UNIX_TIMESTAMP(datetime_column) to fetch the datetime as an integer that you can give to PHP's strftime() function.
DATE_FORMAT('datetime_column', '%M %d, %Y') to format the datetime as a string, and then you don't have to use strftime() to format it.
While I would recommend a completely SQL approach, you can use PHP's DateTime class to process your string in PHP (assuming you're running PHP > 5.3):
// Pick a valid TimeZone
$date_obj = DateTime::createFromFormat( 'Y-m-d H:i:s', '2011-11-26 01:00:27', new DateTimeZone( 'America/New_York'));
echo $date_obj->format('F d, Y');
Demo

How do I convert a date of the format DDMMMYYYY into a db storable date

Hi guys my application parses text files where date values are stored in the format: DDMMMYYYY eg: 12MAR2009.
I noticed that inserting this value as it is into a database datetime value doesn't work at all. How do I convert this into a datetime value which can be entered into a database. My application is in php.
Something like this should convert your date into MySQL's DATETIME format:
$date = new DateTime('12MAR2009', new DateTimeZone('America/New_York'));
$mysql_date = $date->format('Y-m-d H:i:s');
You can use strtotime(), but timezone handling with it is kind of a pain.
$timestamp = strtotime("12MAR2009");
...
INSERT INTO my_table (..., timestamp) VALUES (..., FROM_UNIXTIME($timestamp))
The PHP function strtotime() will take almost any string containing a date/time value, and convert it to a unix timestamp.
The MySQL function FROM_UNIXTIME() will take a unix timestamp and convert it to MySQL's datetime format.

Convert US date format to ANSI SQL date format (YYYY-mm-dd)

I want to convert user-submitted date format (mm/dd/yyyy) to a MySQL date format (YYYY-mm-dd). Submission is via a simple PHP form direc tto MySQL database.
$mysql_date = date('Y-m-d H:i:s', strtotime($user_date));
An alternative method as of PHP 5.2
$datetime = new DateTime($user_date);
echo $datetime->format('Y-m-d H:i:s');
DateTime is Y38k and timezone friendly.
A further method, this time on the SQL side is to use the convert method in your sql query:
CONVERT(VARCHAR(11),$user_date,111)
//e.g.
SELECT CONVERT(VARCHAR(11),DATEFIELD,111) AS DATE
//or
SET DATEFIELD = CONVERT(VARCHAR(11),'".$user_date."',111)
See: http://www.w3schools.com/sql/func_convert.asp - the number at the end changes the type of date format, with 111 returning: 2006/12/30.

Categories