Change date format from dashes to nicer fomrat - php

I have dates that are being stored in the database like this:
08-11-2013
(That's Day, Month year BTW) Is there a way (without changing the database entries) to output this on a page like this instead...
8th November, 2013
Cheers.

FYI, this would be a lot easier if you stored your dates in MySQL standard Date format.
Use MySQL's DATE_FORMAT() with STR_TO_DATE()
SELECT DATE_FORMAT(STR_TO_DATE(datecol, "%d-%m-%Y"), "%D %M, %Y")
But if you have to do it with PHP you can use DateTime::createFromFormat()
$dt = DateTime::createFromFormat('08-11-2013', "d-m-Y");
echo $dt->format('jS M, Y');

While other's answers are valid, this will output the date in the exact format you asked for in your question.
$date = '08-11-2013';
$prettydate = date('jS F, Y', strtotime($date));
echo $prettydate;

In PHP try
<?php
$olddate = '2012-07-18';
$date = new DateTime($olddate);
echo $date->format('jS F, Y');
?>
Also try to read Convert date format yyyy-mm-dd => dd-mm-yyyy
Enjoy coding! :D

Related

Format and echo datetime string received from MySQL?

how would I format a datetime string received from MySQL? For example, when I do
echo $info['start_datetime']
I get 2012-03-18 21:00:00, but I would like to Turn it into Sunday, March 18, 2012. I looked at the php documentation where it showed the different formats, but not specifically when they're retrieved from MySQL.
Thanks everyone!
echo date('l, F d, Y', strtotime($info['start_datetime']));
There are load of ways you can format the date. First change the time into timestamp using strtotime() function.
Check this page to get the list of how to format a date.
The usage is something like this
$time = strtotime($info['start_datetime']);
echo date("l, F d, Y", $time);
Use something like this:
echo ( date( "r", strtotime($info['start_datetime']) ) );
More options here: http://php.net/manual/en/function.date.php

convert integer date into formatted text

I have an sql database with a date column like this yyyymmdd (20111109), i usually sort my return queries by the date.
I would like to know how to convert this to the (D jS M) or (Wed 9th Nov).
For the month, date, and ordinal suffix, im using substr along with case. but its the weekday im getting frustrated with.
Is there a simpler method?
Thanks in advance.
If you are interested in something more sophisticated:
$date = DateTime::createFromFormat('Ymd', '20111109');
echo $date->format('D jS M');
This should be more reliable in the long run.
This should work:
date_default_timezone_set('America/Los_Angeles');
$date = strtotime($your_string);
$formatted_date = date('D jS M', $date);
echo $formatted_date;
Try using strtotime and the date() function
with a combination of those you can achieve:
echo date("D j, M", strtotime(20111109));

How to get AM/PM from a datetime in PHP [duplicate]

This question already has answers here:
Convert from MySQL datetime to another format with PHP
(18 answers)
Closed 2 years ago.
I have a date time in a variable. My format is 08/04/2010 22:15:00. I want to display this like 10.15 PM. How to do this in PHP?
You need to convert it to a UNIX timestamp (using strtotime) and then back into the format you require using the date function.
For example:
$currentDateTime = '08/04/2010 22:15:00';
$newDateTime = date('h:i A', strtotime($currentDateTime));
$dateString = '08/04/2010 22:15:00';
$dateObject = new DateTime($dateString);
echo $dateObject->format('h:i A');
Use strtotime() to make the date a UNIX timestamp.
For output, check out the various options of date().
$timestamp = strtotime("08/04/2010 22:15:00");
date("h.i A", $timestamp);
<?php
$dateTime = new DateTime('now', new DateTimeZone('Asia/Kolkata'));
echo $dateTime->format("d/m/y H:i A");
?>
You can use this to display the date like this
22/06/15 10:46 AM
Like this:
$date = '08/04/2010 22:15:00';
echo date('h:i A', strtotime($date));
Result:
10:15 PM
More Info:
date
strtotime
for flexibility with different formats, use:
$dt = DateTime::createFromFormat('m/d/Y H:i:s', '08/04/2010 22:15:00');
echo $dt->format('g:i A')
Check the php manual for additional format options.
PHP Code:
date_default_timezone_set('Asia/Kolkata');
$currentDateTime=date('m/d/Y H:i:s');
$newDateTime = date('h:i A', strtotime($currentDateTime));
echo $newDateTime;
Output: 05:03 PM
$currentDateTime = $row['date'];
echo $newDateTime = date('l jS \of F Y h:i:s A', strtotime($currentDateTime));
Perfect answer for AM/PM live time solution
<?php echo date('h:i A', time())?>
Just simply right A
{{ date('h:i A', strtotime($varname->created_at))}}
For (PHP >= 5.2.0):
You can use DateTime class. However you might need to change your date format. Didn't try yours.
The following date format will work for sure: YYYY-MM-DD HH-MM-SS
$date = new DateTime("2010-04-08 22:15:00");
echo $date->format("g"). '.' .$date->format("i"). ' ' .$date->format("A");
//output
//10.15 PM
However, in my opinion, using . as a separator for 10.15 is not recommended because your users might be confused either this is a decimal number or time format. The most common way is to use 10:15 PM
It is quite easy. Assuming you have a field(dateposted) with the type "timestamp" in your database table already queried and you want to display it, have it formated and also have the AM/PM, all you need do is shown below.
<?php
echo date("F j, Y h:m:s A" ,strtotime($row_rshearing['dateposted']));
?>
Note: Your OUTPUT should look some what like this depending on the date posted
May 21, 2014 03:05:27 PM

How to reformat date in 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'];
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');
date("F d, Y", strtotime($input))
$new_format = date("Your Date String", strtotime($date));
See:
- http://php.net/strtotime
- http://php.net/date
Basically, if strtotime() can read it correctly, you can reformat it anyway you please.
In this case, Year - Month - Day is a properly recognized strtotime() format, this might not be the case for other formats.
You might consider doing your date formatting in MySQL with your select statement:
DATE_FORMAT(date,'%M %e, %Y') as date_selected
http://www.w3schools.com/sql/func_date_format.asp
<?php
echo date('F j, Y', strtotime($date));
You might want to look at the php function strtotime:
http://php.net/manual/en/function.strtotime.php
It'll parse a large number of date representations to a Unix timestamp.
Then use the date function.
Using strtodate or explode to split the date into its different components, you can then use the date function with the appropriate format string:http://php.net/manual/en/function.date.php
$date = "2009-08-12";
list($year,$month,$day) = explode("-",$date);
$formattedDate = date("F d, Y", mktime(0,0,0,$month,$day,$year));
Outputs: "August 12, 2009"
<?php
//Date Formatter
/*
date: date you want to convert
format: its current format ie m-d-Y, m/d/Y, Y-m-d, Y/m/d
delimS: Current delimiter ie - or / or .
delimF: The delimiter you want for the result
NOTE: this will only convert m-d-Y to Y-m-d and back
*/
function dtform($date,$format,$delimS,$delimF){
$dateFinal = '';
if($format == 'm'.$delimS.'d'.$delimS.'Y'){
$dateFinal_exp = explode($delimS,$date);
$dateFinal = $dateFinal_exp[2].$delimF.$dateFinal_exp[0].$delimF.$dateFinal_exp[1];
}else if($format == 'Y'.$delimS.'m'.$delimS.'d'){
$dateFinal_exp = explode($delimS,$date);
$dateFinal = $dateFinal_exp[1].$delimF.$dateFinal_exp[2].$delimF.$dateFinal_exp[0];
}
return $dateFinal;
}
?>
Use it like this:
// February 1, 2005
print date ("F j, Y", mktime (0,0,0,14,1,2004));

Converting time in PHP?

How do i convert the time which I pull from a database 2009-09-27 23:58:54 to the following time using PHP Sep 27, 2009.
<?php echo date('M j, Y', strtotime('2009-09-27 23:58:54')); ?>
If you're using MySQL then I would advise you use the MySQL date_format() function, something like this:
SELECT date_format(date, '%b %e,%Y') AS `formatted_date` FROM `table_name`;
In PHP you can use strtotime or in MySQL you can use UNIX_TIMESTAMP to get the date into a timestamp.
You can then use the date function to format it as you want:
$timestamp = strtotime($myDate);
$dateStr = date('M j, Y', $timestamp);

Categories