Format string to 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']));

Related

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 time format for UK

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;
?>

Regex to convert already formatted date to different format

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?

Converting ISO 8601 format to d M Y in PHP

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));

How can I format a timestamp from MySQL in PHP?

When I directly echoed a timestamp value I retrieved from a database in PHP, I saw this on my screen.
2012-04-01 12:02:00
What is an efficient way to parse this timestamp string in PHP? Should I use regex or explode to turn this timestamp into two strings and parse each one (date and time)?
Is there a more streamlined and standard way to do this?
In the end, I want to present this timestamp as: 12:02 on April 1, 2012.
date('H:i on F d, Y',strtotime('2012-04-01 12:02:00'))
More info on date pattern here: http://php.net/manual/en/function.date.php
check out the date() and strtotime() functions.
in your example it would be
date("H:i on F d, Y",strtotime("2012-04-01 12:02:00"));
and if you are using a later PHP version than 5.3.0 you could use date_parse_from_format() too. In this case:
$parsed = date_parse_from_format("Y-m-d H:i:s","2012-04-01 12:02:00");
$new = mktime(
$parsed['hour'],
$parsed['minute'],
$parsed['second'],
$parsed['month'],
$parsed['day'],
$parsed['year']
);
date("H:i on F d, Y", $new);
Try this one.
$result = mysql_query("SELECT date FROM times;");
while ( $row = mysql_fetch_array($result) ) {
echo date("g:i a F j, Y ", strtotime($row["date"])) . "<br />";
}
A one-liner would be date_create_from_format('H:i on F d, Y', $timestamp_value)
More on this here: http://www.php.net/manual/en/datetime.createfromformat.php

Categories