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?
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 a date, in the format yyyy-mm-dd that has been pulled from a MySQL database. This date is saved to a variable $myDate.
I want to save the date, three weeks before this variable's date, to a new variable called $otherDate. How can I do this (and ideally, change the format of the date at the same time)?
I tried:
$otherDate = date("l d F", strtotime("-3 weeks", $myDate));
but to no avail.
The best way to get your head around strtotime is to look at the doc page for it:
The format expected is int strtotime ( string $time [, int $now = time() ] )
And the parameters are defined as:
time
A date/time string. Valid formats are explained in Date and Time Formats.
now
The timestamp which is used as a base for the calculation of relative dates.
Therefore $otherDate = date("l d F", strtotime("-3 weeks", $myDate)); is not correct since $myDate is not an integer but a string in yyyy-mm-dd format.
You must pass the $myDate into the string with the -3 weeks modifier:
$otherDate = date("l d F", strtotime("$myDate -3 weeks"));
Or if you want to redefine $now within the strtotime parameters:
$otherDate = date("l d F", strtotime("-3 weeks", strtotime($myDate)));
I would however recommend using a DateTime object:
$otherDate = new DateTime($myDate);
$otherDate->sub(new DateInterval('P3W'));
echo $otherDate->format('l d F');
The second argument to strtotime() must be a timestamp, not a string, so
$otherDate = date("l d F", strtotime("-3 weeks", strtotime($myDate)));
But look at using DateTime objects instead
$date = new \DateTime( $myDate );
$otherDate = clone $date;
$otherDate->sub( new \DateInterval('P3W') );
echo $otherDate->format( 'l d F' );
Since I find procedural date manipulation ugly and constrictive, I'm going to suggest using PHP's awesome DateTime class.
$dt = new DateTime('2014-11-28');
$dt->modify('-3 weeks');
echo $dt->format('l d F');
Because you are using the strtotime wrong way.
Check this:
$otherDate = date("l d F", strtotime("2014-11-28 -3 weeks"));
echo $otherDate;
Output:
Friday 07 November
PHP code:
echo date("c"); //out put like this 2012-06-19T20:37:44+05:30
echo date("d M, Y"); //out put "Jun 19,2012"
$time=date("c");
echo date("d M, Y",$time);// This is not working. What could the reason be?
My requirement is to convert ISO8601 format to 'd M ,Y'. I need it in this format to use with the timeago jQuery plugin.
You need to use the strtotime() function.
echo date("d M, Y",strtotime(date("c")));
Alternatively, you can use PHP's DateTime object to perform the conversion:-
$dateTime = DateTime::createFromFormat(DateTime::ISO8601, date('c'));
echo $dateTime->format('d M Y');
The issue is that the second argument to date is expected to be a timestamp, you are passing a string. Use strtotime to convert your date to into a timestamp:
$time = date("c");
echo date("d M, Y", strtotime($time));
I have already researched a lot of site on how can I convert PHP DateTime object to String. I always see "String to DateTime" and not "DateTime to String"
PHP DateTime can be echoed, but what i want to process my DateTime with PHP string functions.
My question is how can I make PHP dateTime Object to a string starting from this kind of code:
$dts = new DateTime(); //this returns the current date time
echo strlen($dts);
You can use the format method of the DateTime class:
$date = new DateTime('2000-01-01');
$result = $date->format('Y-m-d H:i:s');
If format fails for some reason, it will return FALSE. In some applications, it might make sense to handle the failing case:
if ($result) {
echo $result;
} else { // format failed
echo "Unknown Time";
}
echo date_format($date,"Y/m/d H:i:s");
The simplest way I found is:
$date = new DateTime(); //this returns the current date time
$result = $date->format('Y-m-d-H-i-s');
echo $result . "<br>";
$krr = explode('-', $result);
$result = implode("", $krr);
echo $result;
I hope it helps.
There are some predefined formats in date_d.php to use with format like:
define ('DATE_ATOM', "Y-m-d\TH:i:sP");
define ('DATE_COOKIE', "l, d-M-y H:i:s T");
define ('DATE_ISO8601', "Y-m-d\TH:i:sO");
define ('DATE_RFC822', "D, d M y H:i:s O");
define ('DATE_RFC850', "l, d-M-y H:i:s T");
define ('DATE_RFC1036', "D, d M y H:i:s O");
define ('DATE_RFC1123', "D, d M Y H:i:s O");
define ('DATE_RFC2822', "D, d M Y H:i:s O");
define ('DATE_RFC3339', "Y-m-d\TH:i:sP");
define ('DATE_RSS', "D, d M Y H:i:s O");
define ('DATE_W3C', "Y-m-d\TH:i:sP");
Use like this:
$date = new \DateTime();
$string = $date->format(DATE_RFC2822);
Shorter way using list. And you can do what you want with each date component.
list($day,$month,$year,$hour,$min,$sec) = explode("/",date('d/m/Y/h/i/s'));
echo $month.'/'.$day.'/'.$year.' '.$hour.':'.$min.':'.$sec;
Its worked for me
$start_time = date_create_from_format('Y-m-d H:i:s', $start_time);
$current_date = new DateTime();
$diff = $start_time->diff($current_date);
$aa = (string)$diff->format('%R%a');
echo gettype($aa);
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