1

i have used Now() and it stores something like "2017-01-10 19:28:58" in database which is the current time of user's device.

But i want it like January 10 at 7:28pm . how to do it in simple way. please help

True speaker
  • 362
  • 3
  • 15
  • 1
    read more about this http://www.w3schools.com/php/php_date.asp – Roljhon Jan 10 '17 at 18:41
  • w3schools is an opportunistic organization that provides all kinds of inaccurate and out-of-date information. Many have suggested that it is irresponsible to link to their site. – S. Imp Jan 10 '17 at 19:00
  • Possible duplicate of [Convert one date format into another in PHP](http://stackoverflow.com/questions/2167916/convert-one-date-format-into-another-in-php) – Jann Jan 10 '17 at 20:29
  • @S.Imp - I don't think it is that terrible any more. For many beginners, W3Schools has structured tutorials and playgrounds that offer a decent learning experience. However, it would be a mistake to continue your education without learning from more reputable sources, so when you're ready to level up, move on. – Loaf Jan 10 '17 at 21:20
  • @Loaf For PHP functions, there's no reason to link anything other than php's own docs. http://php.net/date . For Javascript, the authoritative source is the MDN: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date There's plenty of discussion elsewhere on why not to use W3Fools https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date – S. Imp Jan 10 '17 at 21:38
  • @S.Imp I agree that the other docs provide much better information, but W3Schools has improved in recent years, even W3Fools says that. Just saying that it is not irresponsible to link to their site. – Loaf Jan 10 '17 at 21:46
  • 1
    @Loaf I think we might disagree on that. Perhaps we can both agree that it is *more responsible* to link to the canonical documentation when suggesting documentation :D – S. Imp Jan 10 '17 at 21:56
  • @S.Imp I agree, much prefer a link to the canonical docs over W3Schools. – Loaf Jan 10 '17 at 21:57

3 Answers3

0

you could use the PHP date function

date(F d \a\t\ g:ia);
SaggingRufus
  • 1,814
  • 16
  • 32
0

You can use PHP date function with its formatting options

<?php

echo date("F d \a\t g:ia");

If you want more detailed formatting please visit the PHP manual here. You can find everything you need with detailed examples.

Aatish Sai
  • 1,647
  • 1
  • 26
  • 41
0

Your post is ambiguous, but it sounds like you are referring to the SQL NOW() function. The format of the data stored could be a DATETIME or TIMESTAMP or a combination of DATE and TIME columns. The format you get when you retrieve this value from the database depends on that data storage format, your query, and how the value is dealt with when it gets into PHP.

If you want to reformat it using SQL, consider a function like DATE_FORMAT. Assuming your column with the date is called my_column, here's a sample query.

SELECT DATE_FORMAT(my_column, '%M %e at %l:%i%p');

EDIT: you can also use DATE_FORMAT on the result of the NOW() function:

SELECT DATE_FORMAT(NOW(), '%M %e at %l:%i%p');

You might have to tweak the second parameter to get the date format you want.

If your date is stored as a string (VARCHAR or whatever) in your database, then you would need to convert it to a timestamp or datetime first and then use the PHP date function to output the variant you want. Assuming $row is a record from your data table:

$date_string = $row["my_column"];
$stamp = strtotime($date_string); // NOTE that this will assume some timezone
if (!$stamp) {
    die("Could not create a timestamp from the date");
}
echo date("F j \a\t g:ia);

You could also use PHP'S DateTime functions which are more modern, if somewhat verbose in usage.

S. Imp
  • 2,833
  • 11
  • 24