I have date variable i need to convert to a another type of date formats
//the date and time
$date="16-03-2014 00:16:01";
the format i want convert is like below
Sunday 16th of March 00:16:01
can someone please help me to do this.
Use DateTime::createFromFormat to create a Datetime object, then output with specified format.
$date="16-03-2014 00:16:01";
$date = DateTime::createFromFormat('d-m-Y H:i:s', $date);
echo $date->format('l jS \of F H:i:s');
try using this, also set default timezone of UK
<?php
$tm = strtotime("16-03-2014 00:16:01");
echo "<br>".date("l jS \of F H:i:s",$tm);
?>
<?php
$date="16-03-2014 00:16:01";
$uk_date = explode("-", str_replace(" ", "-", $date));
$uk_date_stamp = mktime(0, 0, 0, $uk_date[1], $uk_date[0], $uk_date[2]);
$uk_date = date("l jS \of F Y", $uk_date_stamp)." ".$uk_date[3];
echo $uk_date;
?>
Related
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.
I have $date = $run['at'];, which gives me 2013-06-03T16:52:24Z. How can I manipolate it to get for example "d M Y, H:i" ?
You should use the DateTime class.
$date = new DateTime($run['at']);
echo $date->format('d M Y, H:i');
You can use:
$time = strtotime($run['at']);
echo date("Y-m-d", $time);
But, where does $run['at'] come from?
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
I am having difficulty formatting a string to a human-readable date format.
I have:
07122012
and need to get
7 Dec 2012
Tried:
date("j M Y", strtotime($str));
You can use DateTime::createFromFormat (available with PHP >= 5.3.0) like this:
$date = DateTime::createFromFormat('jmY', '07122012');
echo $date->format('d M Y') . "\n";
try:
$str = "07122012";
$stryear = substr($str,-4);
$strmonth = substr($str,2,2);
$strday = substr($str,0,2);
echo date("j M Y", strtotime($stryear.'-'.$strmonth.'-'.$strday));
it will do the trick, but the best thing is to read the php documentation (probably there is a built in feature)
I think the most elegant way, since you cannot parse this string directly, is this:
$date = "07122012";
// parse the dte fist correctly
$parsed_date = date_parse_from_format('dmY', $date);
// Make time, print date
echo date("j M Y", mktime(0, 0, 0, $parsed_date['month'], $parsed_date['day'], $parsed_date['year']));
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