PHP date conversion dd [th/st/rd] / month / yyyy - php

I have my users entering the date in this format :- mm/dd/yyyy (11/21/2012)
My PHP script converts the date into the following format :- dd-Month-yyyy (21-November-2012)
I do this using :-
$new_date = date('d-F-Y', strtotime($user_date));
How can I have the date in this format :- 21st November 2012?
Thanks

You can use S letter as following:
$new_date = date('jS F Y', strtotime($user_date));
Check manual.

It will output as you expect
$my_date = '2016-01-01';
echo date('F jS, Y', strtotime($my_date));
# January 1st, 2016
while dS will also prepends 0
echo date('F dS, Y', strtotime($my_date));
# January 01st, 2016

$new_date = date('jS F Y', strtotime($date));
S - English ordinal suffix for the day of the month, 2 characters
(st, nd, rd or th. Works well with j)

**My Date = 22-12-1992**
<?php
$mydate = "22-12-1992";
$newDate = date("d M Y", strtotime($mydate));
$new_date = date('dS F Y', strtotime($newDate));
echo $new_date;
?>
**OutPut = 22nd December 1992**

You can use something like:
echo date('l, F jS');
Or even get a bit fancy with the HTML:
echo date('l, F j<\s\u\p>S</\s\u\p>');

You can do that :
<?php echo date('dS M. Y');?>

$date = date_create('09-22-2012');
echo $date->format('d S F Y');
by this code you will get your desire
for more you can also visit http://php.net/manual/en/datetime.formats.date.php

Related

How to Display date as day/month/year

I want to display my date as day/month/year.
example 31st October 2011
echo '<h4>Date Of Birth: '.$row['dob'].'</br></h4>';
You can try below code ::-
echo '<h4>Date Of Birth: '.date('dS F Y', strtotime($row['dob'])).'</br></h4>';
// ^^^^^^^ Here the is format of date, in which format you want.
Output will be ::-
31st October 2011
For more you can refer click here
<?php
$mydate = "2010-03-3";
$newDate = date("d M Y", strtotime($mydate));
$new_date = date('dS F Y', strtotime($newDate));
echo $new_date;
?>
/*Out Put*/
03rd March 2010
So if you have that data stored as or you can convert it to 3 variables representing DD MM YYYY then I'd suggest creating array of months and an array of post-fixes(don't know how to call it, but in your example it's 31st) and getting DD % 10 to use reminder for post-fix array
$month = ['January','February',....,'December']; //remember it's $month[MM-1];
$post = ['th','st','nd',...,'th'] //not sure if this on is correct but hope you get the idea
echo date('dS F Y', strtotime($row['dob']));
Refer to DateTime. I normally avoid strtotime() conversions.
Heres a working solution:
<?php
$date = '2016-11-12';
$newDate = new DateTime($date);
echo $newDate->format('jS F Y');
?>
OUTPUT:
12th November 2016

php change the string into time format

In php I have time like this
$time = '2015-06-29T16:00:00Z';
I want to convert that time like this format Tuesday, December 16, 2015 3:00 PM
For that I tried
echo date( 'jS F Y', strtotime( $time) );
but it is showing time like 1st January 1970
So can someone help me to get the actual time format as I want.
A simple DateTime class usage should suffice, just feed it into the constructor, the just use ->format and provide the desired output format:
$time = '2015-06-29T16:00:00Z';
$date = new DateTime($time);
echo $date->format('jS F Y');
Sample Output
You can use the DateTime class for better handling of dates
$time = '2015-06-29T16:00:00Z';
$dateTime = new DateTime($time);
echo $dateTime->format('l, F d, Y g:i A');
$time = '2015-06-29T16:00:00Z';
echo date( 'l, F j, Y H:i A',strtotime($time));
l, F j, Y H:i A can be re-ordered to change the output.
About date function, http://php.net/manual/en/function.date.php
Just pass proper format parameters to it.
$time = '2015-06-29T16:00:00Z';
echo date( 'l, F j, Y g:i A', strtotime( $time) );
Use preg_split:
$parts = preg_plit("/Z/",$time);
$parts = preg_split("/T/",$parts[0]);
$theDate=$parts[0];
$theTime=$parts[1];
$what_you_want=date(strtotime($theDate." ".$theTime);
Note that you can still change the format of the output.

PHP Date - How to add a string to separate date and time

I want to display date and time format something like this "May 23 at 12:30pm".
I saw in PHP manual and found:
// Prints something like: Monday 8th of August 2005 03:12:46 PM
echo date('l jS \of F Y h:i:s A');
After modification I manage to get
echo date('M j \of h:i a');
it is giving me "May 23 of 12:30pm"
but when i replacing of with at it is giving me "May 23 a23 08:26 pm".
I don't what is going wrong.
you need to escape the a and t as both have special meaning when used as formatting options in date()
echo date('M j \a\t h:i a');
See it in action
Try
<?php
echo date('M j \a\t h:i a');
?>
OR
<?php
echo date('M j'). "at". date(' h:i a');
?>
You need to escape the t too:
echo date('M j \a\t h:i a');
Another option could be:
echo date('M j')." at ".date('h:i a');
You need to try this in php:
$t = now(); //This will give you current time
echo date_format($t,"dS M,y \a\\t h:i a"); //14th May,19 at 05:39am

Convert date integer to text?

Is there anyway to convert the value of idate("z") to a date format that reads the Day, Month, and Year? My code looks like this:
$date_int = idate("z");
$date_text = strtotime($date_int);
$date = date("l, F j, Y", $date_text);
For some reason, it's still echoing Thursday, January 1, 1970.
Any ideas?
idate("z") is incorrect as that will return the day of the year. It seems like you want idate("U"), but in that case just use date() without the second parameter, it will assume time(). Example:
$date = date("l, F j, Y");
That should be all you need.
idate("z") will only return the day of the year. Today being 74. strtotime will not understand 74 as a parsing format. Therefore date() fails all together.
Assuming you want the date of the CURRENT YEAR:
$dayOfYear = 80;
print date("l, F j, Y", strtotime($dayOfYear - idate("z") . " day")); // Wednesday, March 21, 2012
$dayOfYear = 1;
print date("l, F j, Y", strtotime($dayOfYear - idate("z") . " day")) // Monday, January 2, 2012

PHP Convert a Date into this format [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
PHP convert one date into another date format
This is PHP
I have this result:
2011-09-20 13:00:00
I want to convert it into this format:
September 20 2011 1:00 pm
Do you know how to do that?
Anyhelp would be greatly appreciated.
Thanks!
try this:
$old_date = '2011-09-20 13:00:00';
$old_date_timestamp = strtotime($old_date);
$new_date = date('F j Y g:i a', $old_date_timestamp);
If that result comes from an object of type DateTime you can use the format function:
$date = new DateTime('2000-01-01');
echo $date->format('Y-m-d H:i:s');
and here all the formats you can have.. change it according to your needs.
You can get the UNIX-timestamp of a time string with strtotime() and you can get a differently designed time string with strftime()
For more information you can read the documentation here : http://php.net/manual/en/function.date.php
But also is simple. Lets see how
$d = "2011-09-20 13:00:00";
$d = strtotime($d);
$d = date("F m Y g:i a", $d);
echo $d;
Take a look at these PHP functions:
http://nl.php.net/manual/en/function.date.php
and
http://nl.php.net/strtotime
To do something like this:
date('F d Y G:i',strtotime("2011-09-20 13:00:00")); // your required format
You can use this format
$date= date("F j Y g:i a");
if you have date in any variable then
$date= date("F j Y g:i a",strtotime($your_variable));
echo $date
echo date("F d Y g:i a",strtotime("2011-09-20 13:00:00 "));
echo date('F d Y g:i a', strtotime('2011-09-20 13:00:00'));
check the date format

Categories