Formatting time in php like mysql's date_format - php

How can we format time in php like we do using mysql's date_format?
In mySql we do date_format(now(),'%M %e, %Y') as time. Can php's current time be formatted to make it look like January 3, 2013?

Yes. Check out PHP date.
echo date('F j, Y', time());

The code is:
date("Y-m-d",strtotime(date()))
More information on the date() function and some information on the strtotime() function

Related

Change MySQL's datetime into a readable piece of text in PHP

I am storing the time a player joins my server using a datetime datatype and inserting a CURRENT_TIMESTAMP. All this done in MySQL.
This will look something like this: 2014-04-14 02:15:03
However, on my website I want to display the time they join but on a more friendly way such as:
April 14, 2014 2:15CST
In a nutshell, how do I change "2014-04-14 02:15:03" into "April 14, 2014 2:15CST"?
Thanks!
With your mysql timestamp in var $datetime:
$formatted = date("F d, Y H:ie", $datetime);
Refer to date in the PHP manual :)
Try to use it like the following:
date('F d, Y H:i', strtotime($mysqlTimestamp));
For more details, check the PHP date function at: http://php.net/manual/en/function.date.php
You can do this:
SELECT DATE_FORMAT(NOW(),'%M %d %Y %h:%i %p') or SELECT DATE_FORMAT(CURRENT_DATE,'%M %d %Y %h:%i %p')

Formatting datetime

i am trying to format a datetime which comes fromt he database in the format of
2012-06-11 21:39:54
However i want it to display in the format of June 11
How can do this?
Thanks
echo date('M d', strtotime('2012-06-11 21:39:54'));
Output
You can also use DateTime object.
$date = new DateTime($yourString);
$date->format($yourFOrmat);
I think that it would be the best way because DateTime is really more powerful than timestamp and date/strtotime functions.
From the code I gave above you can add functionalities like modifying dates, iterate over the time, compare 2 dates without functions like str_to_time...
$date->modify('+1 day');//the day after for example
foreach(new DatePeriod($date,new DateInterval('PT1M'),10){
$date->format($yourFormat);//iterate each minute
}
and so on
PHP manual gives an excellent documentation about using Date/Time functions. Basically you will need a combination of two functions: strtotime() and date().
strtotime() will convert your date into Unix timestamp which can be supplied to date() as second argument.
The format of date you will need is: M d.
Alternative: In addition you could also try the MYSQL counterpart which won't require conversion to UNIX timestamp. It is documented here. Assuming you are using date as your Datetime field, you will need something like this,
SELECT id,..,DATE_FORMAT(`date`, '%M %d') as f_date FROM table
For formatting date using php, you need to pass timestamp of date
and format specifiers as arguments into date function .
Eg echo date('M d',strtotime('2012-06-11 21:39:54'));

Converting an MySQL Date to friendly string.

I have a date stored in a MySWL database in the following format.
2011-08-23 00:00:00
I'm then pulling that date in with php like so
echo $row['start'];
I want to edit the PHP code to display the date in the following format.
08/16/2011 12:00 am
Is there an easy way to do this?
I prefer:
echo date('m/d/Y g:ia',strtotime($row['start']));
Parse the timestamp stored by MySQL with strtotime to a unix timestamp, then use the date function to format it.
echo date('m/d/Y g:ia', strtotime($row['start']));
date_format is your friend.
http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_date-format
select date_format('2011-08-23 00:00:00','%m/%d/%Y %I:%i %p') -- 08/23/2011 12:00 AM

Displaying Date from database PHP

right now I'm storing a date in the database, in the format date("Y-m-d") and im storing it in date column.
Now, I've retrieved it from the database but how can i display it like
October 31st 2010
Thanks!
Convert the date to a timestamp using strtotime and format it using date.
echo date('F jS Y', strtotime($databaseDate));
The preferred way going forward should be the use of the DateTime class though:
date_default_timezone_set('Asia/Tokyo');
$date = new DateTime($databaseDate);
echo $date->format('F jS Y');
Use date_format in your SQL query.
Example: date_format(somefield, '%d-%m-%Y, %h:%i %p')

PHP Convert ISO date to more readable format?

Just a simple question. How do I convert a PHP ISO time (like 2010-06-23T20:47:48-04:00) to something more readable? Is there a function already built in PHP? I've looked around but I haven't seen anything to convert times. If there's not a function, is it possible?
Thank you
$format = "d M Y"; //or something else that date() accepts as a format
date_format(date_create($time), $format);
Try this:
echo date( "Y-m-d H:i:s", strtotime("2010-06-23T20:47:48-04:00") );
Format this part "Y-m-d H:i:s" using format from this documentation http://php.net/manual/en/function.date.php
echo strftime("%b %e %Y at %l:%M %p", strtotime($ios));
will do
strptime() converts a string containing a time/date with the format passed as second argument to the function. The return value is an array containing values for day, month, year, hour, minutes, and seconds; you can use those values to obtain a string representing the date in the format you like.
strptime() is available since PHP 5.1.0, while the class DateTime is available since PHP 5.2.0.
I think you should try strftime
http://php.net/function.strftime
Sounds like you're looking for the date function: http://php.net/function.date
Possibly paired with the strtotime function: http://php.net/function.strtotime
For a short date try this:
$a_date = '2010-06-23T20:47:48-04:00';
echo date('Y-m-d', strtotime($a_date));

Categories