converting php string with date and time to sql timestamp - php

I have a php string which contains the date and time as shown below
2015-05-27 18:45:31
How can I convert this to a type which I can insert into postgresql table with timestamp data type?

Convert it using strtotime() function:
$unix_timestamp=strtotime('2015-05-27 18:45:31');
Thes use $unix_timestamp in your SQL query.
Also you can use TIMESTAMP '2015-05-27 18:45:31' notation right in SQL statement.

If you have another format of date, you can use DateTime::createFromFormat
In your case:
$dateString = '2015-05-27 18:45:31';
$format = 'Y-m-d H:i:s';
$dateConvert = DateTime::createFromFormat($format, $dateString);
echo "Date: $format; " . $dateConvert->format('Y-m-d H:i:s') . "\n";
The output would be something like:
Date: Y-m-d H:i:s; 2015-05-27 18:45:31

Related

php date to sql

I want to store php date in sql but it store 1970-01-01 05:00: instead of input datetime
This is html form
<input type="datetime" id="eventstart" name="txtEventStart" class="form-control" value="<?php echo date('m/Y/d h:i:s');?>">
when data is sent using post
$date = $this->input->post('txtEventStart');
I convert string to date format using:
$date2 = date('Y-m-d H:i:s', strtotime($date));
It stores '1970-01-01 05:00:00'
I want to know what is the correct format for storing such type of date.
Your code is giving you the wrong results because your date is in a format (m/Y/d h:i:s) that is not recognised by strtotime. Instead, use date_create_from_format to convert it and output a date in the correct form for SQL. For example:
$date = '08/2019/19 10:23:41';
echo date('Y-m-d H:i:s', strtotime($date)) . PHP_EOL;
echo date_create_from_format('m/Y/d h:i:s', $date)->format('Y-m-d H:i:s');
Output:
1970-01-01 01:00:00
2019-08-19 10:23:41
Demo on 3v4l.org
'YYYY-MM-DD hh:mm:ss' is not an acceptable format for PHP date function. Use Y-m-d H:i:s instead. More info here: https://www.w3schools.com/php/func_date_date.asp
Update: For date input field use ISO date format Y-m-d or change input type to 'text'

Mysql format date to insert to table

I need to format and insert the date to mysql table which is type of datetime, I am doing this from php.
Here is the statement
$timestamp ="22-11-2015+00:00:00";
$mysqltime = date ("d-m-Y H:i:s", $timestamp);
But when I insert to table the date is wrong, it showing 31-12-1969 19:00:22
What could be the issue?
It should be
$timestamp ="22-11-2015+00:00:00";
$mysqltime = date ("d-m-Y H:i:s", strtotime($timestamp));
because date is expecting it's second parameter to be a number, specifically the number of seconds since midnight January 1st, 1970.
The date function expects an integer timestamp for the second argument.
This MSQL insert statement would work fine:
INSERT INTO mytable (birthday) ('2015-11-22 00:00:00')
If you're using a database class in PHP and the function you're calling is using a PHP date object, then you'd have to construct the PHP date object using the date() function.
Or if you want to use PHP code to reformat the date in PHP before sending the string to the database to be inserted, then I think you'd use strtodate to get a PHP date object, and then date()
$phpdate = strtodate('d-m-Y H:i:s', '22-11-2015 00:00:00')
$dateformysql = date('Y-m-d H:i:s', $phpdate)
databaseclass.dotheinsert($mysqldate)
This should be working, but did not work for me!
$timestamp ="22-11-2015+00:00:00";
$mysqltime = date ("d-m-Y H:i:s", strtotime($timestamp));
I believe it is because my national standard format (LC_TIME) is "Y-m-d" rather then "d-m-Y". However this works perfectly:
$timestamp ="22-11-2015+00:00:00";
// The '+' sign needs to be escaped!!
$time = date_create_from_format( "d-m-Y\+H:i:s", $timestamp);
$mysqltime = $time->format( 'd-m-Y H:i:s' );
On the other hand, MySQL use the date format "YYYY-MM-DD" as a standard. The correct format string should be:
$mysqltime = date ("Y-m-d H:i:s", strtotime($timestamp));
// or second option
$mysqltime = $time->format( 'Y-m-d H:i:s' );
I think the corret format would be:
$mysqltime = date_format ($timestamp,"d-m-Y H:i:s");
Let me know if this works

How to turn a string like "16/Sep/2014 08:34" to unix timestamp in PHP

Struggling without much success to turn strings like "16/Sep/2014 08:34" extracted from an array with explode command to unix timestamp like "2014-09-17 05:32:05" in PHP. Any help, please?
Edit: With #Erik's help I finally got the right result:
$date = DateTime::createFromFormat("d/M/Y H:i", $line[0]);
$date = $date->format('Y-M-d H:i');
$timestamp = strtotime($date);
You'll need to use DateTime::createFromFormat and then convert the resulting datetime to a timestamp by using $datetime->getTimestamp();
--
// this will create a generic PHP date object, which you can then manipulate into anything you want
$date = DateTime::createFromFormat( "d/M/Y H:i", "16/Sep/2014 08:34" );
// this will generate a unix timestamp (which is an integer)
$timestamp = $date->getTimestamp();
// this will generate the string you request in your question
$string = $date->format( "Y-m-d H:i:s" );
--
For more info on formatting dates, check out the PHP documentation: http://php.net/manual/en/datetime.createfromformat.php

Intricacies on DateTime

Why is this odd behaviour ?
I want to create this date 2009-02-15 to my specified format, but it shows a year of 1970 . Why is this happening ? I found this example on PHP.net manual.
<?php
$format = 'Y-m-!d H:i:s';
$date = DateTime::createFromFormat($format, '2009-02-15 15:16:17');
echo "Format: $format; " . $date->format('Y-m-d H:i:s') . "\n"; //Output is Format: Y-m-!d H:i:s; 1970-01-15 15:16:17
That's because of the !. From the manual for DateTime::createFromFormat:
If format contains the character !, then portions of the generated time not provided in format, as well as values to the left-hand side of the !, will be set to corresponding values from the Unix epoch.
This means that anything left of the ! will be set to 1970-01-01 00:00:00 UTC. In your case (Y-m-!d H:i:s) that means that the year and month will be set to January 1970, while the remaining parts of the date will be set according to your string.
Why is it ! sign in precise format? It should not be there. Correct format is Y-m-d H:i:s, so:
$format = 'Y-m-d H:i:s';
$date = DateTime::createFromFormat($format, '2009-02-15 15:16:17');
-see manual page, it explains meaning of ! (and you need not have it in precise format)
Check It
$date = date('Y-m-d H:i:s',strtotime('2009-02-15 15:16:17'));
echo $date;

Converting dates with PHP for DATETIME in SQL

I have a forum in PHP which takes a date like in the form
dd/mm/yyyy hh:mm:ss. However, I need to insert it for SQL as a DATETIME in the format as yyyy-mm-dd hh:mm:ss. How can I convert this data?
Your date time format is wrong: dd/mm/yyyy hh:mm:ss. Probably you mean d/m/Y H:i:s
If you have 5.3+ version there is safe way to convert the date time into another format. Here's an example:
$timestamp = '31/05/2001 12:22:56';
$timestamp = DateTime::createFromFormat('d/m/Y H:i:s', $timestamp);
echo $timestamp->format('Y-m-d H:i:s');
or if you like more procedural way:
$timestamp = '31/05/2001 12:22:56';
$timestamp = date_create_from_format('d/m/Y H:i:s', $timestamp);
echo date_format($timestamp, 'Y-m-d H:i:s');
Be careful with previous suggestions. Some are completely wrong and others could lead to errors.
You can use the strtotime and date to rework the format.
$new_date = date( "Y-m-d H:i:s", strtotime( $old_date ) );
What this does is take your old date (dd/mm/yyyy hh:mm:ss), converts it to a unix timestamp that can then be used with the php date function to format the date to the desired format.
Two of several possible ways:
Convert in code and then pass the converted value to mysql: $mysqldate = date( 'Y-m-d H:i:s', $phpdate );
Let mysql do the work by using its built-in functions:
$query = "UPDATE table SET datetimefield = FROM_UNIXTIME($phpdate) ...";
if you have datetime avaialable from a from like above format then u just need to use following function.
function localToMysql($dateTime){
$date_chunks = explode('/', $dateTime);
$time_chunks = explode(' ', $date_chunks[2]);
$final_format = $time_chunks[0] . "-" . $date_chunks[1] . "-" . $date_chunks[0] . " " . $time_chunks[1];
return $final_format;
}

Categories