php date to sql - php

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'

Related

Parse Date in PHP

I need some help parsing a date in PHP to a date object. I thought this was 8601 format, but $date = DateTime::createFromFormat(DateTime::ISO8601, "2018-11-14T01:11:36.059Z") is not working.
Date Format is: 2018-11-14T01:11:36.059Z
Try This:
echo date('Y-m-d H:i:s', strtotime('2018-11-14T01:11:36.059Z'));
//Output
2018-11-13 20:11:36

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

converting php string with date and time to sql timestamp

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

Issue converting from Y/m/d to Y-m-d using $date = date('Y-m-d H:i:s', $date);

I'm trying to convert the brackets to hyphens, but instead the date variable loses its value:
echo $date; // outputs 26/05/2015 10:41:56sd2
$date = date('Y-m-d H:i:s', $date);
echo $date; // outputs 969-12-31 18:00:26
The second parameter to date() must be a Unix timestamp. You're giving it a string.
That date format is invalid and won't work with strtotime() anyway. When you use / as the date separator US format is assumed. There is no 26th month.
The last three characters of that is not valid in any standard that I know of and will break any date function unless you specifically account for it (which you can't do with date() or strtotime())
Use DateTime::createFromFormat() to do this:
$date = DateTime::createFromFormat('d/m/Y H:i:s???', '26/05/2015 10:41:56sd2');
echo $date->format('Y-m-d H:i:s');
Demo

how to store date and time in mysql php

I have one field displaying date and time in following format:
30/01/2011, 4:57 pm
I want to store this in mysql using data type datetime.
After getting value from post, I try to store it in database but its storing 0000-00-00 00:00:00 in table.
How can I store above specified values?
You can use date and strtotime like this:
$date = '30/01/2011, 4:57 pm';
$to_mysql_date = date('Y-m-d h:i:s', strtotime(str_replace('/', '-', $date)));
echo $to_mysql_date;
Result:
2011-01-30 04:57:00
Working Example
Now you can use the $to_mysql_date variable to insert the date in MySQL-friendly format :)
More Info:
http://php.net/manual/en/function.date.php
http://php.net/manual/en/function.strtotime.php
If you don't want to change the formatting of your date you can always go for a VARCHAR field, but you're going to miss the purpose of datetime. Or you can transform that to a timestamp and use the TIMESTAMP field.
Alternatively, you can use:
$sd = DateTime::createFromFormat('d/M/Y, H:i a', '30/01/2011, 4:57 pm');
$mysqldate = date('Y-m-d H:i:s', strtotime($sd->format('Y-m-d H:i:s')));

Categories