Solved

Modifying display of MySQL Timestamp data in PHP

Posted on 2007-04-08
3
189 Views
Last Modified: 2013-12-12
I am pulling a value from a MySQL timestamp column into a php script. When It displays nicely, but there are two problems I need to fix. First, it's an hour behind since my hosting company is in a different time zone. Second, it displays military time instead of 12 hour time. So, how can I mold the displayed data? In other words,  the current display is "2007-04-08 14:25:02" but I want it to read as "2007-04-08 3:25:02 PM". What is the best way to accomplish this? Thank you so much!
0
Comment
Question by:smetterd
  • 2
3 Comments
 
LVL 8

Accepted Solution

by:
Yuval_Shohat earned 250 total points
ID: 18873362
the format of the timestamp would be:
select DATE_FORMAT(YourTimeStamo,'%Y-%m-%d %r') from YourTable
the time zone is bit more tricky. check this link from the mysql manual:
http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html#function_convert-tz

havent checked it out but it should be something like:
select DATE_FORMAT(CONVERT_TZ(YourTimeStamp,'Host's TimeZone','Your TimeZone'),''%Y-%m-%d %r') from YourTable

-=Yuval=-
0
 
LVL 50

Assisted Solution

by:Steve Bink
Steve Bink earned 250 total points
ID: 18873572
Since you're using PHP, you might want to take a look at the functions on that side of the coin:

http://www.php.net/manual/en/ref.datetime.php

Specifically:

http://www.php.net/manual/en/function.date-default-timezone-set.php

PHP will manage the time zone information for you so long as you tell it what to expect at the begining.  Set your default timezone according to your server's time, then use the time zone-related PHP functions to automatically adjust times and timestamp fields.
0
 
LVL 50

Expert Comment

by:Steve Bink
ID: 18873574
To change the default format in which date/times are displayed in PHP, check this function:

http://www.php.net/manual/en/function.date.php
0

Featured Post

Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article discusses four methods for overlaying images in a container on a web page
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
The viewer will learn how to dynamically set the form action using jQuery.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question