Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to reformat date in PHP?

Tags:

date

php

I am pulling the dates of various posts from a database. The dates are in the following format:

2009-08-12  Numeric Year - Numeric Month - Numeric Day 

How can I reformat these dates to something more user friendly like:

August 12, 2009  Numeric Month Numeric Date, Numeric Year 

Assuming that the date gotten from the mysql database is stored in a variable called:

$date = $row['date_selected']; 
like image 496
Lisa Avatar asked May 02 '10 20:05

Lisa


People also ask

How can I get current date in YYYY-mm-DD format in PHP?

date_default_timezone_set('UTC'); echo "<strong>Display current date dd/mm/yyyy format </strong>". "<br />"; echo date("d/m/Y"). "<br />"; echo "<strong>Display current date mm/dd/yyyy format</strong> "."<br />"; echo date("m/d/Y")."<br />"; echo "<strong>Display current date mm-dd-yyyy format </strong>".

How can I change the date format?

Press CTRL+1. In the Format Cells box, click the Number tab. In the Category list, click Date. Under Type, pick a date format.

How do I format in PHP?

CTRL + ALT + F You can change this if desired through the standard Keyboard Shortcuts (File > Preferences > Keyboard Shortcuts) option screen the name is "Format HTML in PHP".


1 Answers

Unlike the strtotime based examples, this allows you to ensure the month and day are interpreted in the correct order regardless of locale settings specified on the server.

$date = DateTime::createFromFormat('Y-m-d', '2009-08-12'); $output = $date->format('F j, Y'); 
like image 126
Billy ONeal Avatar answered Oct 02 '22 11:10

Billy ONeal