1

How would go about adding AM/PM to the ending of the following code snippet? So for it outputs like this: 04/21/2012 07:59.

<?=mdate('%m/%d/%Y %H:%i', strtotime($row->entryCreationDateTime))?>
Michael Grigsby
  • 11,467
  • 9
  • 33
  • 52

2 Answers2

3

Unless you're trying to use this in a query, you could use date. From the documentation, AM/PM are given by "A":

<?php echo date('m/d/Y H:i A', strtotime($row->entryCreationDateTime)); ?>

As an aside, it's generally a good idea to avoid short tags in your code.

Community
  • 1
  • 1
rjz
  • 16,182
  • 3
  • 36
  • 35
  • This makes them all AM though when some are PM as well. How do I figure out if it's Am or Pm? – Michael Grigsby May 10 '12 at 05:03
  • "A" stands in for AM or PM in the `date` function...maybe a problem with the timestamps you're using? Can you give an example (i.e., the value of `$row->entryCreationDateTime`, before it's parsed with `strtotime`)? – rjz May 10 '12 at 05:06
  • So that one *is* an AM..do you have one of the should-be-PMs that isn't rendering correctly? – rjz May 10 '12 at 05:12
  • Oh I'm sorry lol I didn't notice I just added that one. Here you go: 2012-04-21 07:57:58 – Michael Grigsby May 10 '12 at 05:15
  • 1
    Hmm, still an AM. Unless it's marked AM/PM in the timestamp or running on a 24-hour clock (e.g., `19:57:58`) it's going to be tough for `strtotime` to guess which one you mean. – rjz May 10 '12 at 05:33
  • I'm using NOW() in the sql insertion statement. What other function could I use that'll add the am/pm to the end? – Michael Grigsby May 10 '12 at 05:41
  • NOW() should be fine -- it operates on the 24 hour clock. – rjz May 10 '12 at 05:45
  • So why I'm getting AM and not PM is unknown. I guess I'll just wait till the pm and test to see if it really works or not. I'm pretty sure I didn't post of my posts in the am though – Michael Grigsby May 10 '12 at 05:59
  • Actually it is working. I've noticed all of my post dates are the same. I was debugging something and I had to go in and make all the post dates the same. I pulled some much older posts in and the PM is showing up. Thanks for your help! – Michael Grigsby May 10 '12 at 06:03
  • Quick question though, if I replace 'A' with 'P', what does that do? – Michael Grigsby May 10 '12 at 06:04
  • From the documentation, "P: Difference to Greenwich time (GMT) with colon between hours and minutes (added in PHP 5.1.3)". So, something else entirely. – rjz May 10 '12 at 13:34
1

I believe it's

# 'AM' or 'PM' - upper case 'A'
<?=mdate('%m/%d/%Y %H:%i A', strtotime($row->entryCreationDateTime))?>

or

# 'am' or 'pm' - lowercase 'a'
<?=mdate('%m/%d/%Y %H:%i a', strtotime($row->entryCreationDateTime))?> 
Kevin Bedell
  • 13,254
  • 10
  • 78
  • 114