473,748 Members | 11,145 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Formatting the MySQL Timestamp in php

RT
If anyone can help that would be great.

Iım trying to format a timestamp from my MySQL table (sessions)

Hereıs the code Iım using:

<?php echo date('D,n-j-y h:i:s a',strtotime($r ow_rsSessions['date'])); ?>
If I give the timestamp a value of 8 I can get the date to work correctly
but if itıs at 10 or 12 I get the wrong date and it comes out the same for
every record * Mon,38-1-18-09:14:07 pm

Anyone have any thoughts ?

Thanks
Jul 17 '05 #1
4 7081
RT wrote:
This message is in MIME format. Since your mail reader does not understand this format, some or all of this message may not be legible.


Please don't MIME us.
Iım trying to format a timestamp from my MySQL table (sessions)

Hereıs the code Iım using:

<?php echo date('D,n-j-y h:i:s a',strtotime($r ow_rsSessions['date'])); ?>
If I give the timestamp a value of 8
8? ?!?!?!?
I can get the date to work correctly
but if itıs at 10 or 12
10?, 12? ?!?!?!?!
I get the wrong date and it comes out the same for
every record * Mon,38-1-18-09:14:07 pm
If I'm not mistaken that's "2038-01-19 02:14:07 UTC"
-- the limit for UNIX_TIMESTAMPs
Anyone have any thoughts ?
Let's see ...

~$ php -r 'echo date("Y-m-d H:i:s", strtotime("2004 1026163628")), "\n";'
1970-01-01 00:59:59

oops -- if you MySQL returns something like the above it will not work.
~$ php -r 'echo date("Y-m-d H:i:s", strtotime("2004-10-26 16:36:28")), "\n";'
2004-10-26 16:36:28

Aha! This works!
~$ php -r 'echo date("Y-m-d H:i:s", "20041026163628 "), "\n";'
2038-01-19 03:14:07

oops
~$ php -r 'echo date("Y-m-d H:i:s", "2004-10-26 16:36:28"), "\n";'
1970-01-01 00:33:24

oops
Let's now check MySQL ...
~$ mysql -upedro -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 350 to server version: 4.0.21-log

Type 'help;' or '\h' for help. Type '\c' to clear the buffer.

mysql> select unix_timestamp( "20041026163828 ");
+----------------------------------+
| unix_timestamp( "20041026163828 ") |
+----------------------------------+
| 1098805108 |
+----------------------------------+
1 row in set (0.01 sec)

mysql> exit
Bye
~$ php -r 'echo date("Y-m-d H:i:s", "1098805108 "), "\n";'
2004-10-26 16:38:28

Aha! Maybe it's best to select UNIX_TIMESTAMPs and let MySQL and PHP
worry about dates.

Thanks

<snip MIME stuff>

You're welcome.
--
USENET would be a better place if everybody read: | to mail me: simply |
http://www.catb.org/~esr/faqs/smart-questions.html | "reply" to this post, |
http://www.netmeister.org/news/learn2quote2.html | *NO* MIME, plain text |
http://www.expita.com/nomime.html | and *NO* attachments. |
Jul 17 '05 #2
RT wrote:
If anyone can help that would be great.

Iım trying to format a timestamp from my MySQL table (sessions)

Hereıs the code Iım using:

<?php echo date('D,n-j-y h:i:s a',strtotime($r ow_rsSessions['date'])); ?>
If I give the timestamp a value of 8 I can get the date to work correctly
but if itıs at 10 or 12 I get the wrong date and it comes out the same for
every record * Mon,38-1-18-09:14:07 pm

Anyone have any thoughts ?

Thanks

What do you mean by 'give the timestamp a value of 8'?

If you feed the value 8 (or 10, or 12) as a timestamp for the date
function you should not get a sensible result. (Should be sometime on
Jan 1st 1970)

A timestamp starts at Jan 1st 1970 (roughly) and goes thru to 19th Jan
2038. Looks like your result doesn't come from the format string you
quoted but from 'D,y-j-n-h:i:s a' and that it is an adjustment back from
the max value for a timestamp.

Personally I would also reccommend using date formatting functions in
your SQL statement.
Jul 17 '05 #3
.oO(RT)

Please don't post HTML.
Iım trying to format a timestamp from my MySQL table (sessions)
What type do you use in your database for storing the dates?
Hereıs the code Iım using:

<?php echo date('D,n-j-y h:i:s a',strtotime($r ow_rsSessions['date'])); ?>

If I give the timestamp a value of 8 I can get the date to work correctly
but if itıs at 10 or 12 I get the wrong date and it comes out the same for
every record * Mon,38-1-18-09:14:07 pm


Not sure what you mean, but you might want to have a look at MySQL's
date and time functions, especially DATE_FORMAT().

Micha
Jul 17 '05 #4
There is a MySQL specific function called "FROM_UNIXTIME( )" which will
convert an epoch date number, to a standard SQL formatted date.

You're probably more comfortable with seeing dates formatted like that...
RT wrote:
If anyone can help that would be great.

I’m trying to format a timestamp from my MySQL table (sessions)

Here’s the code I’m using:

<?php echo date('D,n-j-y h:i:s a',strtotime($r ow_rsSessions['date'])); ?>
If I give the timestamp a value of 8 I can get the date to work
correctly but if it’s at 10 or 12 I get the wrong date and it comes out
the same for every record – Mon,38-1-18-09:14:07 pm

Anyone have any thoughts ?

Thanks

Jul 17 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
32426
by: Ben | last post by:
I would like to use php to query a database and retrieve a unix timestamp. The problem is that mysql is storing the data in the date format and not a timestamp. I am sure that I can amend my query to format the date returned as a timestamp without having to do the conversion in php. Can someone tell me what to put in my db query?
2
4570
by: RT | last post by:
How would I format a timestamp which is used to show when a quote was created in a record set. This is the echo - <?php echo($row_rsquote); ?> I tried doing this - <?php echo date('D, m/d/Y',$row_rsquote); ?> but all I get is Monday, 01/18/2038.
0
3946
by: Mike Chirico | last post by:
Interesting Things to Know about MySQL Mike Chirico (mchirico@users.sourceforge.net) Copyright (GPU Free Documentation License) 2004 Last Updated: Mon Jun 7 10:37:28 EDT 2004 The latest version of this document can be found at: http://prdownloads.sourceforge.net/souptonuts/README_mysql.txt?download
2
2167
by: johndcal | last post by:
Hello All, I have a date value that I pull from a .csv file. After reading the file and storing the values in an array the value of the date could be found in $array, for example. ============================================================= while (($data = fgetcsv($handle,5000, ",")) !== FALSE) { $mydate = $data; // here is the array value that holds the date }
2
1673
by: laredotornado | last post by:
Hello, Using PHP 4, MySQL 4, I am getting a date field $dbh = executeSQL($query); while ($row = mysql_fetch_array($dbh)) { $offer_id = $row; $dep_date = $row; // print out date print $dep_date;
2
2711
by: David | last post by:
Hi, Has anyone had this problem ? I am using MySQL ODBC 3.51 Driver, with MS Access 2003 and MySQL 4.1.11 standard log. I created my tables in MS Access, then exported them via ODBC to an externally hosted MySQL database (fasthosts) . I then import-linked
6
38943
by: Brandon | last post by:
I'm using PHP with MySQL 4.x and was having trouble converting a datetime from MySQL into a formatted string until I ran across this solution that converts a YYYY-MM-DD HH:MM:SS string into a Unix-style timestamp and then formats it. $timestamp = "2005-04-06 15:43:34"; $time = strtotime($timestamp); print date('Y-m-d \a\t H:i', $time)."\n"; However, it seems kind of counter productive. After all, aren't people
3
1625
by: mantrid | last post by:
Hello I have date and time in my mysql table in the form 2007-05-03 00:00:00 which I have dispayed on my webpage using echo $selldatetime I want to know how I can display it in the form 03-05-2007 Thanks
10
4023
by: WebCM | last post by:
There is a function: http://paste.ubuntu.com/21865 It needs GMT date in YYYY-MM-DD HH:MM:SS format - in SQL: datetime. If date is the same as today, the function returns "Today". There is one problem. This function does not recognize time zones. How to adjust date to user's time zone? Is converting to timestamp() and then to format readable to visitors the one and only solution (e.g. strtotime() + date() OR DateTime object)? Perhaps,...
0
8989
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8828
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9537
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8241
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6795
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6073
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4599
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4869
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2780
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.