Tilted Forum Project Discussion Community  

Go Back   Tilted Forum Project Discussion Community > Interests > Tilted Technology


 
 
LinkBack Thread Tools
Old 07-12-2004, 07:36 PM   #1 (permalink)
Psycho
 
FaderMonkey's Avatar
 
Location: Orlando, FL
[PHP/MySQL] formatting the date

I'm still just learning PHP and MySQL and I'm having a problem understanding how to print out the date of a table entry. I can print it out so that it looks like this:

20040712112214

....but how do I format it so that it makes sense? I know there is a DATE_FORMAT function in MySQL, but I guess I don't know how to use it cause I can't get it to work.
FaderMonkey is offline  
Old 07-12-2004, 07:59 PM   #2 (permalink)
Junkie
 
Always store your timestamps as a Unix date integer and use the PHP date() function to format it as human-readable text.

http://us2.php.net/manual/en/function.date.php
SinisterMotives is offline  
Old 07-12-2004, 09:08 PM   #3 (permalink)
Psycho
 
FaderMonkey's Avatar
 
Location: Orlando, FL
The type of field I have my date/time setup as is TIMESTAMP. When I'm defining and running my query, I'm doing it like this:

$query = 'SELECT * FROM weblog2 ORDER BY entrydate DESC'
if ($r = mysql_query ($query))
while ($row = mysql_fetch_array ($r)) {
print ...

How do I go about using the PHP date() function? Where do I go from here?
FaderMonkey is offline  
Old 07-13-2004, 12:23 AM   #4 (permalink)
Crazy
 
Location: Salt Town, UT
SinisterMotives: I thought I was the only one that knew that secret... now it's out of the bag, everybody is going to be able to do time easily.... now if they just could not learn about setting the locale and printing the date normally to adjust for timezone settings, I could have cornered the market.

FaderMonkey: A great way to handle TIMESTAMP columns is either to alter them into INT columns that are unix timestamps, or if that isn't an option, on each and every select and insert, you can use UNIX_TIMESTAMP(column_name) (for selects) and FROM_UNIXTIME(timestamp) (for inserts/updates). MySQL's date/time functions are really lacking, so I normally just avoid them and use PHP all of the way. It's a lot easier, but still harder than Postgres's full timezone support for it's date/time columns.
Rawb is offline  
Old 07-13-2004, 07:40 AM   #5 (permalink)
Junkie
 
Quote:
Originally posted by FaderMonkey
How do I go about using the PHP date() function? Where do I go from here?
The date() function requires a Unix time integer as its second argument, so it won't work with the MySQL timestamp. You'll need to use the MySQL UNIX_TIMESTAMP function in a query to get the timestamp into Unix format, as aoeuhtns suggests.

To answer your question - how to use the date() function: the first argument to date() is a string built up of the flags shown in the table on the manual page I linked you to. Example:

PHP Code:

<?php
  $unix_time 
mktime();
  echo 
"The Unix representation of the current date and time is " $unix_time ."<br>";
  
$human_time date("F j, Y, g:i a"$unix_time);
  echo 
$human_time;

?>
would print:

Code:
The Unix representation of the current date and time is 1089736421
July 13, 2004, 10:33 am
SinisterMotives is offline  
Old 07-13-2004, 08:27 AM   #6 (permalink)
Psycho
 
FaderMonkey's Avatar
 
Location: Orlando, FL
Quote:
Originally posted by SinisterMotives
You'll need to use the MySQL UNIX_TIMESTAMP function in a query to get the timestamp into Unix format, as aoeuhtns suggests.
Please excuse my ignorance, but how do I go about doing that? So far I'm defining my query like this:

PHP Code:
$query 'SELECT * FROM weblog2 ORDER BY entrydate DESC' 
How do I add the UNIX_TIMESTAMP function?
FaderMonkey is offline  
Old 07-13-2004, 08:55 AM   #7 (permalink)
Junkie
 
You'll probably have to replace the asterisk with the actual names of the fields:

PHP Code:
$query 'SELECT field1, field2, field3, UNIX_TIMESTAMP(entrydate) FROM weblog2 ORDER BY entrydate DESC'
I've never actually worked with MySQL timestamps, so I don't know if that will work correctly in your case. That's just how I think it should work.
SinisterMotives is offline  
Old 07-13-2004, 10:50 AM   #8 (permalink)
Psycho
 
FaderMonkey's Avatar
 
Location: Orlando, FL
That worked. Thank you, thank you, thank you, thank you, thank you for your help.
FaderMonkey is offline  
Old 07-13-2004, 11:54 AM   #9 (permalink)
Psycho
 
FaderMonkey's Avatar
 
Location: Orlando, FL
Oops...I guess I was wrong. The date() function is printing the current time instead of the timestamp from the data base. Here's how I wrote it:

PHP Code:
$datetime date("F j, Y, g:i a, $entrydate"); 
Is there something else I'm missing?
FaderMonkey is offline  
Old 07-13-2004, 12:35 PM   #10 (permalink)
Junkie
 
The second double-quote goes after the a in the first argument. $entrydate is the second argument, not part of the first argument.
SinisterMotives is offline  
Old 07-13-2004, 12:40 PM   #11 (permalink)
Junkie
 
You're going to have to get the MySQL timestamp into a Unix date integer format before you can use date() on it too. There's no telling what the result will be if you just supply $entrydate as an argument to date().
SinisterMotives is offline  
Old 07-13-2004, 12:55 PM   #12 (permalink)
Psycho
 
FaderMonkey's Avatar
 
Location: Orlando, FL
When I do this:

PHP Code:
 $query 'SELECT UNIX_TIMESTAMP(entrydate), entrytitle, entrytext FROM weblog2 ORDER BY entrydate DESC LIMIT 10';

while (
$row mysql_fetch_array ($r)) {

 
$datetime date("F j, Y, g:i a", {$row['entrydate']);

print 
"<p><br><b>{$row['entrytitle']}</b></p>
<p>
{$row['entrytext']}</p>
<p>
$datetime</p>";} 
I get a parse error for the line with the date() function in it.

When I do this:

PHP Code:
 $query 'SELECT UNIX_TIMESTAMP(entrydate), entrytitle, entrytext FROM weblog2 ORDER BY entrydate DESC LIMIT 10';

while (
$row mysql_fetch_array ($r)) {

$datetime date("F j, Y, g:i a"$entrydate);

print 
"<p><br><b>{$row['entrytitle']}</b></p>
<p>
{$row['entrytext']}</p>
<p>
$datetime</p>";} 
...it prints the date as December 31, 1969, 4:00 pm.
FaderMonkey is offline  
Old 07-13-2004, 02:04 PM   #13 (permalink)
Fluxing wildly...
 
MrFlux's Avatar
 
Location: Auckland, New Zealand
In that first one there you have a { before $row['entrydate'] which is causing the parse error.
__________________
flux (n.)
Medicine. The discharge of large quantities of fluid material from the body, especially the discharge of watery feces from the intestines.
MrFlux is offline  
Old 07-13-2004, 02:26 PM   #14 (permalink)
Psycho
 
FaderMonkey's Avatar
 
Location: Orlando, FL
Quote:
Originally posted by MrFlux
In that first one there you have a { before $row['entrydate'] which is causing the parse error.
Yep, that fixed the parse error, but it's still printing the date as December 31, 1969, 4:00 pm.
FaderMonkey is offline  
Old 07-13-2004, 02:34 PM   #15 (permalink)
Junkie
 
The date() function usually returns a representation of 12/31/1969 if the supplied Unix integer is not a valid date on or after midnight 01/01/1970. The problem is further up in your code. Not sure where.
SinisterMotives is offline  
Old 07-13-2004, 02:36 PM   #16 (permalink)
Junkie
 
Oh. Try this:

PHP Code:
$datetime date("F j, Y, g:i a"$row['entrydate']); 
SinisterMotives is offline  
Old 07-13-2004, 03:57 PM   #17 (permalink)
Psycho
 
FaderMonkey's Avatar
 
Location: Orlando, FL
I ended up doing it this way and it seems to be working:

PHP Code:
$query ="SELECT entrytitle, entrytext, entrydb,";
$query.=" DATE_FORMAT(entrydate, '%M %d, %Y %T') AS date";
$query.=" FROM weblog2 ORDER BY entrydate DESC LIMIT 10";
$result=mysql_query($query);
while (list(
$entrytitle,$entrytext,$entrydb,$entrydate) = 
mysql_fetch_row($result)) 
I have another question about this though. I got this code from someone else....a website about making a simple weblog....so I want to understand exactly how it's working. The seperate lines for defining the query ($query= and then $query.=)....is that just a way of setting different queries to the same variable? Does that question make sense?
FaderMonkey is offline  
Old 07-13-2004, 04:20 PM   #18 (permalink)
Junkie
 
The dot operator simply concatenates the three strings. It could have been written:

PHP Code:
$query ="SELECT entrytitle, entrytext, entrydb, DATE_FORMAT(entrydate, '%M %d, %Y %T') AS date FROM weblog2 ORDER BY entrydate DESC LIMIT 10"
SinisterMotives is offline  
 

Tags
date, formatting, php or mysql


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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -8. The time now is 07:42 PM.

Tilted Forum Project

Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2024, vBulletin Solutions, Inc.
Search Engine Optimization by vBSEO 3.6.0 PL2
© 2002-2012 Tilted Forum Project

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360