Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How do I display the mysql stored date in my page in any format I choose?

PHP & MySQL

How do I display the mysql stored date in my page in any format I choose?

by  spencertaylor  Posted    (Edited  )

The date values to pass from PHP code to the date field in your table should be:

date('Y-m-d-H-i-s')

This is the format that mysql holds the date in. So pass this value to a variable and include it in your INSERT or UPDATE queries:

$today = date('Y-m-d-H-i-s');

Then the query is of the form:

UPDATE mytable SET mydatefield=$today

A nice way to display the entered date in your php pages is as follows, assuming you have the date field included in your SELECT query:
Code:
$date = $queryfieldarray['mydatefield']; [color green]//get the date from the field in my table[/color]
$dateArray = explode("-",$date); [color green]//explode this value using -[/color]
$year = $dateArray[0]; [color green]// the first exploded value[/color]
$month = $dateArray[1]; [color green]// the second exploded value[/color]
$daytime = $dateArray[2]; [color green]// the third which includes the day as a number and time as hours,minutes and seconds are separated by :[/color]
$datetimeArray= explode(" ",$daytime); [color green]// these are separated by a space so explode that to separate the day from the time[/color]
$day=$datetimeArray[0];
$time=$datetimeArray[1];
echo $day.'/'.$month.'/'.$year.' at '.$time;  [color green]//display the UK date style[/color]

Hope this helps everyone.
Register to rate this FAQ  : BAD 1 2 3 4 5 6 7 8 9 10 GOOD
Please Note: 1 is Bad, 10 is Good :-)

Part and Inventory Search

Back
Top