How to insert AM and PM in Mysql Database? - php

This is my value, I am going to insert in my DB
[phoneinterview] => 2018-05-16 10:28 PM
In My databse I have field like this
phoneinterview timestamp
But Its Inserting Like this:
2018-05-16 10:28:00
Actually I want to insert like this:
2018-05-16 10:28 PM

If you use a datetime field in your database you can't, but if you change the field to a varchar then you are able to save it the way you like it. I would recommend against it. Why would you want to save a formatted time? It is best practice to save the date in a datetime field, and then when you want to show it format the way you want to. In Laravel you can do this using a mutator like this:
public function getPhoneinterviewAttribute() {
return \Carbon\Carbon::now(
$this->attributes['phoneinterview']
)->format('g:i A');
}
Plus side: When you are expanding to areas where people don't use AM/PM then you can easily format it to their locale.

Ok. When you are selecting timestamp or datetime datatypes for storing date, it does not store the AM and PM values in the database table fields. The best solution for this is you retrieve the date and convert that into date and time along with AM/PM manually.
One alternate solution is using the datatype of varchar instead of timestamp but that is not recommendable.

MySQL uses the format 'Y-m-d H:i:s' to store dates, so if you're using anything other than that, you need to convert it to this format first. This can be done with DateTime::createFromFormat():
$date = DateTime::createFromFormat('Y-m-d g:i a', '2018-05-16 10:28 PM');
This will create a DateTime object that you can easily format for MySQL:
$date->format('Y-m-d H:i:s');

As you are using datetime datatype it's storing 2018-05-16 10:28:00. But if you want to store AM/PM you need to use varchar datatype.
I suggest you to use ->format(); while you get the timestamp data. This will convert the date time format as your wish.
For now, as you want to display AM/PM after time, use it like: ->format('g:i A'); This will show time like 10:28 PM.
Hope this helps you!!

Related

Insert date in specific format into MySQL db

I need to insert the current date in the following format into a TIMESTAMP column in a MySQL db: d-m-Y
As of now I am using SQL NOW(), which returns the date as Y-m-d. Because I am using AJAX to display the data I cannot format the returned result using $date_returned->format(d-m-Y). Therefore I need to insert the date in the format that I will display on my AJAX call.
I tried to insert the date using the following functions:
1) date('d-m-Y');
2) (new \DateTime())->format('Y-m-d');
I understand these two functions do pretty much the same thing but I was not sure what else I should try.
MySQL threw the following error for both dates:
Error : (1292) Incorrect datetime value: '-2014' for column 'msg_date' at row 1
I am guessing this should be an easy fix but I can't figure out what is wrong.
I tried both TIMESTAMP and DATETIME on MySQL's end but neither worked. (I need it to be TIMESTAMP though).
Any suggestion is welcome!
$newdate= date('Y-m-d', strtotime('10-09-2015'));
or if you want current time just use
$now = date('Y-m-d');
If your msg_date column's structure is DATETIME or TIMESTAMP, the date format should be:
YYYY-MM-DD HH:MM:SS
which can be formatted through PHP like this:
$date = date("Y-m-d H:i:s");
Or if you already have a date, and you want it to convert to that format, we can use strtotime():
$date = date("Y-m-d H:i:s", strtotime($date));
For more date format, check this link.
The MySQL error message indicated that you had the date format the wrong way around.
Year must go first, then month, then day, as in:
date('Y-m-d') // right
In your first example, you have
date('d-m-Y') // wrong
In one of your examples above, you have it right, but you say you got the same response, so I assume that was not what you actually tried.
Another thing to note is that a MySQL TIMESTAMP column stores both a date and time. It's valid to give MySQL just a date (MySQL will just leave the time at zero), but if you have no need to store a time, you may as well make the column DATE instead of TIMESTAMP.
If you want to display your dates as d-m-Y then by all means do so, but they need to be sent to MySQL as Y-m-d.

how to echo out 12 hour time format in my page

please i need your help i created a comment box in my page, stored inside phpmyadmin with time type DATETIME. the problem am having is the time always display in 24 hour format and i want it to display in 12 hour format (PM/AM) and be stored inside mysql. i have tried using date() function at the same time i used date("y-m-d H:i:s") instead of now())function but the result i keep on getting is in 24 hour format
see the code
$insert = mysql_query("INSERT INTO test (name,comment,whenadded) VALUE ('$name','$comment', now())");
With this code i get the result in 24 hour time format.
whenadded is the DATETIME variable name.
thank you in advance.
You want to store the date as DATETIME in the MySQL DB, thats good practice.
For output, use PHP's date() function. Look at this answer. Or you use MySQLs date_format() function.
SELECT date_format(whenadded, 'Y-m-d h:i') AS my_date FROM ...
The php documentation should help http://www.php.net/manual/en/function.date.php
what you are looking of is date(y-m-d g:i a) wich will give something like "2013-12-30 4:38 pm"
Let the mysql decide its date format, it's mostly irrelevant for you.
What you need, is to properly format your output data, like:
echo date("y-m-d h:i:s A", strtotime($date));
Where $date is the variable you get from MySQL.
In no particular order:
phpMyAdmin is not a database engine. MySQL is.
Dates are not stored in any particular format. You give format when you convert them to strings.
The mysql_... legacy extension is deprecated, insecure, triggers a notice in latest PHP versions and will be removed.
Your code is probably vulnerable to SQL Injection.
The H format code means: 24-hour format of an hour with leading zeros.
It's good to save the data within 24Hours Format, but you can show it within 12Hours plus am/pm
date("d/m/Y - g:i A");

php date to mysql returns 0's

Hello i am trying to add a date time from PHP into my MySQL database, in the database I have tried date time and time stamp and I have tried it with and without current_time set as the default option, in my php I have the following.
date_default_timezone_set("Europe/London");
$date = date('m/d/Y h:i:s a');
My hope was to just add the $date into the value's part of the upload query however in all cases it comes back as 0's, it echo's the date fine, however it uplaod's 0's, any help is appreciated, Thanks.
'm/d/Y h:i:s a is not a standard MySQL datetime format so unless you are storing it as varchar/char you will get the results you are seeing.
Your options are to:
Store the date in a standard format (datetime Y-m-d H:i:s, timestamp) and convert it to whatever format you want during the query (recommended)
Store it as a string but lose all of the datetime functionality MySQL offers. Losing this functionality will make working with dates in your queries and PHP very painful and is not recommended to do.

Convert PHP datetime to MySQL RFC-822 valid Date-Time for RSS Feed

I want to add a date/time, created in PHP, to MySQL that is valid in a RSS Feed.
I'm using in PHP
$d = date( 'Y-m-d H:i:s T', time() );
$mysqldate = gmdate(DATE_RSS, strtotime($d));
inserting that into a DATETIME field in my database
But it saves it in this format Wed, 02 Oct 2002 08:00:00
and it need to be in this format to be RFC-822 valid Wed, 02 Oct 2002 08:00:00 EST
Use DATE_RFC822 instead of DATE_RSS
Why are you converting a php date time to a string, then converting that string back into a datetime object again? that's a serious waste of cpu cycles. why not simply do
$mysqldate = gmdate(DATE_RSS, time())?
as well, gmdate generates a UTC timestamp, for which there is no timezone - it's always GMT+0
Answer is very simple just use this format:
<pubDate>'.date('r', strtotime($rss_row['your_date_field'])).'</pubDate>
The original echo is:
echo date('r', strtotime($my_date));
This turns the datetime from inside our MySQL table which is formatted like: YYYY-MM-DD HH:MM:SS and turns it into a UNIX string that represents the same date.
Once we do that to our date, we have the date() function’s many wonders at our disposal. Going into those many wonders is more than I’ll get into here, but I’ve posted about it here.
In this particular case, adding the ‘r’ parameter to the date() function does all the work of reformatting our UNIX string into our RSS feed date format.
It’s that easy.
When saving something into a MySQL DATETIME field, you can not specify the format. DATETIMEs are stored in an internal format that is only concerned with the time value, not with the formatting. You will always have to format the date the way you need it after (or while) retrieving it from the database:
date(DATE_RSS, strtotime($dateFromDatabase));
To insert a date into the database you just need to provide it in a format MySQL understands, it will then be converted to said internal format and the formatting will be lost:
sprintf("INSERT INTO `foo` (`date`) VALUES('%s')", date('Y-m-d H:i:s'))
date('r', strtotime($object->pubDate))
-works nice
Actually DATETIME fields don't save any information about the timezone (http://dev.mysql.com/doc/refman/5.1/en/datetime.html).
So you need to append the timezone AFTER fetching the date from the database.

Date conversion from db to display

I'm reading a datetime field from mysql db. I'd like to convert it in PHP:
from: 2009-05-23 14:46:23
to 05/23/2009 02:46 pm
Notice the am/pm conversion.
Thanks in advance..
// assume you retrieve the mysql date in variable $date
date("m/d/Y h:i a", strtotime($date));
Call me old fashioned. I store all dates as epoch, and if I will ever go outside my timezone, as epoch of UTC of the date.
Epoch is seconds since Jan 1st, 1970 or something like that.
Known in MySQL as Unixtime. FROM_UNIXTIME(), etc..
$epoch = strtotime($mdate);
You can also store dates directly in MySQL with data type DATETIME if you are so inclined.
Your chosen format is a drop in replacement actually.
Depending on the type that's return by your db code, you can create a new DateTime object or format an existing one with date_format(). From the example you give, the date format string should be "m/d/Y h:i a". Note that the conversion from 24h to 12h is handled in either case.
Have a look at the format options for DateTime:date() and DateTime:format() (date_format is an alias for this)

Categories