Insert h:mm pm/am time format into database using MYSQL - php

I'm trying to insert a time written in h:mm am/pm format into a database that is stored as standard DATETIME format ( hh:mm:ss ) but I can't figure out how to convert the posted time into standard format so the database will accept it.
This is what I've been trying so far:
$title = $_POST['inputTitle'];
$date = $_POST['inputDate'];
$time = date('h:i:s a', strtotime($_POST['inputTime']));
$desc = $_POST['inputDesc'];
//msql query to insert data
$query = "INSERT INTO events(title, date, time, description) VALUES ('$title','$date','$time','$desc')";
but this doesn't work(time still won't submit) any ideas?

You should use TIME type not DATETIME type.
DATETIME format is: yyyy-mm-dd hh:ii:ss
DATETIME format
TIME format is: hh:ii:ss
TIME format

Related

Unable to save the exact timestamp from form

I'm trying to save events into my table that contain date_start and date_end timestamps. From another PHP page, I get four inputs: two date inputs and two time inputs. I'm trying to insert these values but I keep getting the following value on inserting it:
0000-00-00 00:00:00
Here's my script:
$rep=$bdd->query('SELECT * FROM association WHERE nom="'.$_SESSION['usertag'].'"');
$data=$rep->fetch();
$debut="\'".$_POST['date_start']." ".$_POST['time_start'].":00\'";
$fin="\'".$_POST['date_end']." ".$_POST['time_end'].":00\'";
$debut=date('Y-m-d H:i:s');
$fin=date('Y-m-d H:i:s');
$timestamp_debut =strtotime($debut);
$timestamp_fin = strtotime($fin);
$req=$bdd->query('INSERT into evenement values (NULL,'.$_POST['name'].','.$_POST['content'].',\''.$timestamp_debut.'\',\''.$timestamp_fin.'\','.$data['id'].')');
PHP's strtotime() outputs a Unix timestamp. If your database column type is DATETIME, a Unix timestamp is not the correct format. You can just insert the formatted date() string.
For example:
strtotime(date('Y-m-d H:i:s'))
1562117846
date('Y-m-d H:i:s')
2019-07-02 21:36:40
For MySQL:
MySQL recognizes DATETIME and TIMESTAMP values ... [a]s a string in either 'YYYY-MM-DD hh:mm:ss' or 'YY-MM-DD hh:mm:ss' format. A "relaxed" syntax is permitted here, too: Any punctuation character may be used as the delimiter between date parts or time parts. For example, '2012-12-31 11:30:45', '2012^12^31 11+30+45', '2012/12/31 11*30*45', and '2012#12#31 11^30^45' are equivalent.
String and Numeric Literals in Date and Time Context
However, it seems that you're using the current timestamp when you might want to use the values posted from your form. PHP's date() uses "the current time if no timestamp is given".
If you want to use your posted values instead, you can indeed use strtotime() to convert them to Unix timestamps and then date() to format them.
$date = '2019-07-02';
$time = '15:28';
date('Y-m-d H:i:s',strtotime($date.' '.$time));
2019-07-02 15:28:00
Alternatively, I might recommend using PHP's DateTime class:
$date = '2019-07-02';
$time = '15:28';
$datetime = new Datetime($date.' '.$time);
echo $datetime->format('Y-m-d H:i:s');
2019-07-02 15:28:00

Incorrect value of datetime PHP and MySQL

In my site, I have a bootstrap datepicker which allows user to pick date in format of MM/DD/YYYY (e.g: 05/12/2014). Then when this data is submitted, I used the following PHP code to convert it into Datetime type, then insert into start_date (DATETIME datatype) column in MySQL .
$start_date = date('Y-m-d', $_POST['start_date']);
the insert query in PHP does nothing with reformatting the date. It just simply insert into corresponding column.
However, instead of inserting '2014-05-12', the value inserted into database is '1970-01-01'. That's so weird to me. Can anybody tell me what's wrong here. Is this that I used incorrect PHP function or incorrect timezone setting or ...
Just do this:
$start_date = date('Y-m-d', strtotime($_POST['start_date']));
You could also use strtotime() on your $_POST.
$start_date = date('Y-m-d', strtotime('05/12/2014'));
try to use
$date = str_replace('/', '-', $_POST['start_date']);
$start_date = date('Y-m-d', strtotime($date));
For more :- Converting between illogically formatted dates (changing /slash/ to -dash- )

strtotime () not converting to timestamp

I have a loop and in it the date is coming in different formats like for some values it will be like '10-13-2013 04:31' and for some it is like '2013-10-14T22:14:40-0700'. I tried to store this in DB as the value of a datetime/timestamp column but it is failing for the first format that is 10-13-2013 04:31. So I tried to convert it into UNIX timestamp using strtotime(). It is working for some values and is storing zero for values like '10-13-2013 04:31'. I think this is because it is considering the second value as month and so failing. My code is as follows :
foreach($reports as $report){
echo strtotime($report->transactionDate);
}
strtotime() is unable to parse mm-dd-yyyy format. Instead you should use DateTime::createFromFormat(), like this:
$date = '10-13-2013 04:31';
$obj = DateTime::createFromFormat('m-d-Y H:i', $date);
$date = $obj->format('Y-m-d H:i:s');

Datetime format PHP and MySQL

I have the following problem. I have a string, which contains time and date as follows:
dd/MM/yy hh:mm:ss
This I have in PHP. And then I have a MySQL database, which has a column of a datetime format, where I need to insert this value. Obviously the problem is, that the format is different, so instead of the actual date, it results in a field "0000-00-00 00:00:00".
Could you please help me with converting this string and then inserting it properly into MySQL?
For the MySQL I use the standard INSERT INTO command.
From the DATETIME documentation:
MySQL retrieves and displays DATETIME values in YYYY-MM-DD HH:MM:SS format.
I'd use PHP's DateTime class and DateTime::createFromFormat() method, and convert the data into a MySQL-compatible date string, like so:
$date = DateTime::createFromFormat('d/m/Y H:i:s', $yourDateString);
$dateToBeInserted = $date->format('Y-m-d H:i:s');
Write a function to convert date,
function sqldate($date)
{
$sql_date = date('Y-m-d H:i:s',strtotime($date));
return $sql_date;
}
And your query look like,
$query = "INSERT INTO tableName (dateColumn) VALUES('".sqldate($date)."') ";
You can convert it using STR_TO_DATE() along with your INSERT statement, eg.
INSERT INTO tableName (dateColumn)
VALUES(STR_TO_DATE('dd/MM/yy hh:mm:ss','%d/%m/%y %H:%i:%s'))
SQLFiddle Demo
Try this:
$parsedDate = date('Y-m-d H:i:s', strtotime($yourDate));
// Insert $parsedDate into your table column Date

date format in mysql?

Hello every one i have a problem with mysql dbms i never had a date storage in this format yyyy-mm-dd i always have it in this format dd-mm-yyyy i used this methode to change the date format while inserting data but it does not work
function dateConvert($dateInser) {
$dateTime = new DateTime($dateInser);
$dateFormate=date_format ( $dateTime, 'Y-m-d' );
return $dateFormate;
}
is there any solution ?
The DateTime constructor also expects specific formats, and I doubt dd-mm-yyyy is one of them. First parse your dd-mm-yyyy date to transform it into a DateTime object, and then format this DateTime object using the yyyy-mm-dd format :
$dateTime = date_create_from_format('d-m-Y', $dateInser);
$dateFormate = date_format($dateTime, 'Y-m-d');

Categories