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')
Related
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
I have dates formatted as d/m/y. How can I insert them into a DATETIME column?
Use MySQL's STR_TO_DATE() function:
INSERT INTO my_table VALUES (STR_TO_DATE('26/5/12', '%e/%c/%y'))
You need to use php's date() function along with strtotime() to convert date to any format you want.
MySQL database stores the date in YY-MM-DD format for datetime datatype, so if for example you have a date
$date = '26/05/2012';
You can convert it by using date() and strtotime()
$formatDate = date('Y-m-d', strtotime('26/05/2012'));
This will convert the date from 26/05/2012 to 2012-05-26 which then can be inserted into the database.
If you are using a timestamp datatype to store the date in your database, then all you need is to convert the current date into unix timestamp and store in database for example.
$date = strtotime('26/05/2012');
//this will convert the date to unix timestamp
Update:
as pointed out by #wallyk (thank you), strtotime() does not handles dd/mm/yy format. the fix is to replace the slash / by -m below code should work for you.
date('Y-m-d', strtotime(str_replace('/', '-', '26/05/2012')));
Try this:
$mysqldate = date("m/d/y g:i A", $datetime);
$date = date('d/m/Y');
$date = strtotime($date); //in unix time stamp format
Basically american date format is MM/DD/YYYY and you are providing DD/MM/YYYY so thats why startotime() returns you a null values on this input; and i prefer you must follow standard date format of american (MM/DD/YYYY) because if you are using mentioned format of date that will create more problems as well in different places ..
if you check by this
echo date('Y-m-d', strtotime('05/26/2012') );
and it is working fine ..
you could change your DATE column into a String Column and insert the data when ever you want 2 check if the date is right you can use a regular expression to do so
I store the date and time in mysql as a date/time field which has this format: 2012-03-12 14:51:26, what i am trying to do is simply rearrange the DD/MM/YY to look like this.
When i use the following code, it just gives me a date wrong format warning.
echo date_format($date, 'Y-m-d H:i:s');
If you are just displaying the date you can supply a certain format in the SQL query
SELECT DATE_FORMAT("%d/%m/%Y", date_column) FROM table
If you convert the MySQL timestamp to a unix timestamp, then you can use the date() function to output it in whatever format you like:
$unixTimestamp = strtotime($mysqlDate);
echo date($dateFormat, $unixTimestamp);
See the date format strings here: http://php.net/manual/en/function.date.php
First, convert it to a Unix timestamp (which I find to be all around better than a date_time field for a lot of reasons), then use PHP's date function.
echo date('Y-m-d h:i:s', strtotime($date));
Simply do:
date("d/m/Y", strtotime($date));
And read about strtotime function.
this will work.
$date = date_create("2012-03-24 17:45:12");
echo date_format($date, 'Y-m-d H:i:s');
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
I have dates stored in a mysql table, they are set to store as CURRENT TIMESTAMP in each row and are stored as follows:
2010-05-29 01:17:35
but what i am trying to do is somehow use PHP to be able to seperate everything and create a date more like:
May 29 2010 1:17 AM
can anyone at least direct me in the right path that i should take. any help is greatly appreciated!
echo date('M j Y g:i A', strtotime('2010-05-29 01:17:35'));
http://php.net/manual/en/function.date.php
You have two solutions :
Use strtotime() to parse the date to a timestamp, and date() to re-format it to a string
Or use the DateTime class
In PHP code, this would mean using :
echo date('M j Y g:i A', strtotime('2010-05-29 01:17:35'));
Or :
$dt = new DateTime('2010-05-29 01:17:35');
echo $dt->format('M j Y g:i A');
strtotime + date is the solution you'll see used the most ; but it is not the best solution : with those, you'll work on UNIX Timestamps, which means a limited range of dates (from 1970 to 2038, if using 32 bits integers).
ON the other hand, using the DateTime class, there will be no limit to the range of dates you can work with.
If you have a DATETIME field in your table and you only want the date field, then:
SELECT DATE_FORMAT(timestamp,'%M %D, %Y') FROM Mytable;
or:
SELECT DATE_FORMAT(timestamp,'%Y-%m-%d') FROM Mytable;
where timestamp is your column name.