GIDForums  

Go Back   GIDForums > Computer Programming Forums > MySQL / PHP Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 13-Jun-2005, 16:00
Snapple Snapple is offline
New Member
 
Join Date: Jun 2005
Posts: 16
Snapple is on a distinguished road
Question

Problem displaying TIMESTAMP to people


Hello again. My project is coming along fine but now I have one very simple problem. I have a timestamp set to $regtime that, for this example, is 20050613152659 . Obviously I don't want to display it like that so I did this PHP command...
$regtime = date("l M dS, Y, H:i:s",$regtime);
and it comes out as
Monday Jan 18th, 2038, 21:14:07
(it should say something like Tuesday June 13th, 2005, (hour:min:sec))

Now I'm going to work on that cron job
Also I would like to take this space to say thank you for all of your help, this forum has made my php experiance a lot smoother.
  #2  
Old 13-Jun-2005, 22:39
JdS's Avatar
JdS JdS is offline
Senior Member
 
Join Date: Aug 2001
Location: KUL, Malaysia
Posts: 3,371
JdS will become famous soon enough
Hello Snapple,

The second parameter in the PHP date() function is expecting a unix timestamp number while you have been supplying it with a MySQL timestamp value. This is why the function returns an invalid date.

There are quite a few ways to do what you want to do but I will only show you 2 and then it simply boils down to what you will prefer to do.

One way is to format the date (data) on the fly, in the SQL query itself. Assuming the column name for the date in the database table is `regtime` for example, you could rewrite the query to something like this:

Code:
SELECT *, DATE_FORMAT(`regtime`, '%W %M %D, %Y, %H:%i:%s') AS `regtimef` FROM `members` WHERE `id` = 1

In your PHP script, you will just refer to $row['regtimef'] to show the date which is already formatted the way you want it displayed on a web page.

The second way is for you to use PHP's own strtotime() function to convert the MySQL timestamp into a Unix timestamp value.

PHP Code:

<?php

// assume you have just queried the database and have the MYSQL timestamp 
// stored inside the variable: $regtime which is for example, "20050614091407"

$regtime  =  strtotime( $regtime ); // convert to UNIX timestamp
$regtime  =  date( "l M dS, Y, H:i:s", $regtime );  // now regtime is e.g. "Tuesday June 14th, 2005, 09:14:07"

?>

  #3  
Old 14-Jun-2005, 09:55
Snapple Snapple is offline
New Member
 
Join Date: Jun 2005
Posts: 16
Snapple is on a distinguished road
Hello JdS,
Something is wrong strtotime($regtime) changes it to -1 so the time is displayed as "Wednesday Dec 31st, 1969, 17:59:59". I even tried strtotime(20050613152659) and got the same result. So I did a little research on converting to Unix timestamp and found a code that works for me.
PHP Code:

//from timestamp to unix
$regtime='20050613152659';
$year=substr($regtime,0,4);
    $month=substr($regtime,4,2);
    $day=substr($regtime,6,2);
    $hour=substr($regtime,8,2);
    $minute=substr($regtime,10,2);
    $second=substr($regtime,12,2);
    $regtime=mktime($hour,$minute,$second,$month,$day,$year);
    $regtime  =  date( "l M dS, Y, H:i:s", $regtime ); //displays Monday Jun 13th, 2005, 15:26:59
//end 


thank you again.
  #4  
Old 14-Jun-2005, 10:14
JdS's Avatar
JdS JdS is offline
Senior Member
 
Join Date: Aug 2001
Location: KUL, Malaysia
Posts: 3,371
JdS will become famous soon enough
Oh really? I am sorry, I didn't verify my code example before posting it I took for granted that the strtotime() function is quite an intelligent function :-P

That "fix" you pasted in your last reply looks really convoluted...

I suggest that you add this to your SQL statement everytime you are working with MySQL timestamps then:

Code:
SELECT *, UNIX_TIMESTAMP(`regtime`) AS `regtimeUTS` FROM `members` WHERE `id` = 1

That way you will always have the extra $row['regtimeUTS'] in UNIX timestamp format (to do what you will with it in the PHP date() function).
 
 

Recent GIDBlogOnce again, no time for hobbies by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Graphic problem in Unreal Tournament 2004 zerox Computer Software Forum - Games 10 09-Oct-2005 13:31
commands out of sync problem (c++ builder) Largowww MS Visual C++ / MFC Forum 0 28-May-2005 04:40
String problem vaha C Programming Language 3 24-May-2005 19:21
Need advice on a disturbing problem JUNK KED Open Discussion Forum 6 31-Mar-2005 14:51

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 00:55.


vBulletin, Copyright © 2000 - 2009, Jelsoft Enterprises Ltd.