How can I format a timestamp from MySQL in PHP? - 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

Related

Format string to date

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

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

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

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

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

Categories