how to echo out 12 hour time format in my page - php

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");

Related

How to concatenate timestamp in php received from the database?

So I have a timestamp from mysql database in a format: 2016-08-15 15:35:53
I receive it like this: $row['date'] and I want to have just 15:35 for example, i.e. HH:MM format. Would be even better if it were a 12-hour format.
I assume it is passes the whole timestamp as a string?
Thank you!
Why not do it in MySQL directly? strtotime is a handy function, but it's also a waste of CPU resources, as you'll be forcing mysql and PHP to take the mysql internal datetime value, format it to a string, which you then convert to a php unix timestamp, and then convert BACK to string.
Just do the conversions ONCE:
SELECT time(yourfield) FROM ...
SELECT DATE_FORMAT('%H:%i', yourfield) FROM ...
Relevant docs: https://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_date-format
Solved with:
$time = date('H:i', strtotime($row['date']));
I don't know if I should delete the question... Thanks splash58

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.

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.

PHP Storing a date in my SQL for an appointment

This is easy and i did search for it and I gotn really complicated answers or things that didn't match my question exactly it's really simple
I have the date
$day = "Jaunuary 8th 2014 5:00pm";
I got this date from a POST from a forum, the user can only select a few dates and time so the value is controlled.
What is the best way to INSERT day and time?
like this? 04-18-2011 or 20091228 for day and what about time?
When I retrieve this information the goal is so i can sort it by time and date so that i can print it out in ORDER OF date
I should probably INSERT date and time together as one variable correct?
Why complicate things?
MySQL has the syntax of "YYYY-MM-DD HH:MM:SS" for timestamps. Luckily, PHP's date function can handle this quite well:
// the POST variable you retrieved, converted to a time via strtotime(), then converted to a date via date()
$appt = date('Y-m-d H:i:s',strtotime($_POST['appointment_datetime']));
PHP's date and strtotime functions are smart enough to interpret everything correctly and translate it to the format you need to INSERT into MySQL:
"INSERT INTO someTable (AppointmentDate) VALUES ('$appt');"
Then, when you retrieve it from the DB, you reformat it (showing the basic mysqli syntax rather than using proper binding, just for ease of explanation):
$appt = mysqli_query($link,"SELECT AppointmentDate FROM someTable;");
while($apptRow = mysqli_fetch_array($appt,MYSQL_ASSOC)){
echo date('F j, Y g:i a',strtotime($apptRow['AppointmentDate']));
}
This will echo the "plain english" version of the dates. This is obviously simplistic, you would likely capture it into a variable and display appropriately, but you should get the gist. You can consult the PHP date function documentation to learn the appropriate symbols you can use, if you want to have a different format.
Hope this helps. :)
Store a Unix Timestamp and then you can display it in any format you like
$ts=time(); // Store that value in an INT (11)
Then you can display it like
echo date("F j, Y, g:i a",$YourStoredTimestamp); // eg March 10, 2001, 5:16 pm

uk date format in mysql

i'm having trouble getting the date to be imported into mysql from my form.
my form is validated so that the input will always be dd/mm/yyyy
i'm currently using
$date = date('Y/m/d', strtotime($_POST['night_attending']));
this takes the value from the form and assigns it the year/month/day
the problem is that its reading my form as mm/dd/yyyy
i can swap the code to 'Y/d/m' which will put it into my database the right way round but it will stop working if the day is past the 12th as it still believes that it is the month
i have tried using
date_default_timezone_set('Europe/Dublin');
and
date_default_timezone_set('Europe/London');
but it makes no difference
i'm contemplating just using the mm/dd/yyyy format on my form, but this isn't great as it's a uk site.
has anybody encountered this problem? i'm sure it must be comman and there must be a simple answer that i'm missing.
thanks
alsweet
There is no means to set MySQL's date format - the options exist, but they aren't enforced/used.
I don't recommend storing the dates as VARCHAR in order to maintain your format -- use the MySQL format, and work with MySQL date functions (IE: STR_TO_DATE, DATE_FORMAT) to output the format you want for screen.
Be aware that dates will be dependent on the timezone of the host MySQL is on - you might want to consider using epoch timestamps instead, depending on your needs.
This will give you the mysql date format from UK date.
$timestamp = strtotime(str_replace('/', '.', '03/04/2011'));
$mysql_date = date('Y-m-d', $timestamp); // 2011-04-03
You can use strptime() to parse the date:
$dateArray = strptime('%m/%d/%Y');
or date_parse_from_format() on PHP 5.3.
EDIT: I saw this too in the comments for strtotime, might help.

Categories