Solved

Modifying display of MySQL Timestamp data in PHP

Posted on 2007-04-08
3
186 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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Foreword In the years since this article was written, numerous hacking attacks have targeted password-protected web sites.  The storage of client passwords has become a subject of much discussion, some of it useful and some of it misguided.  Of cou…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

707 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now