0

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.

Ed Gibbs
  • 25,924
  • 4
  • 46
  • 69

5 Answers5

1

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 ...
Community
  • 1
  • 1
aebersold
  • 11,286
  • 2
  • 20
  • 29
  • Give OP also an option to format datetime in MySQL, using `DATE_FORMAT()` function. – Glavić Dec 30 '13 at 15:37
  • @aebersold tnx but pls am i gonna insert it like this echo date_format(whenadded, 'Y-m-d h:i') AS my_date FROM test – user3144774 Dec 31 '13 at 10:30
  • I'm not sure what your question is. 1. Store the date as DATETIME in the DB. 2. Format the output, with the examples I provided. – aebersold Dec 31 '13 at 11:54
0

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"

  • you are correct that's what i want, but how can it be stored in mysql. you know if i output this date(y-m-d g:i a) it will keep on updating when ever i refresh my page. which i dnt want it to be so, cause it is a comment box. pls help me out. tanx allot – user3144774 Jan 04 '14 at 12:53
0

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.

Eternal1
  • 5,447
  • 3
  • 30
  • 45
0

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.
Álvaro González
  • 142,137
  • 41
  • 261
  • 360
-3

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");
Goikiu
  • 574
  • 3
  • 12