junglyboi Posted November 20, 2006 Share Posted November 20, 2006 I am having some trouble parsing a mysql timestamp into a php date().In my 'promoters' table, I have a timestamp column which defaults to now() so that when I update a record, the timestamp updates, and I have a "last update" value to work with.I'm using php to create an xml page. Here's my code:[code]<?phpheader("Content-type: text/xml");// index.phpecho "<url>"; echo "<loc>http://www.ticketstone.com/</loc>"; echo "<lastmod>"; $mod = date('Y-m-d\TH:i:s-05:00', strtotime(now)); echo $mod; echo "</lastmod>"; echo "<changefreq>hourly</changefreq>"; echo "<priority>0.7</priority>";echo "</url>";// events.php q=all "All Current Events"echo "<url>"; echo "<loc>http://www.ticketstone.com/events.php?q=all</loc>"; echo "<lastmod>"; $mod = date('Y-m-d\TH:i:s-05:00', strtotime(now)); echo $mod; echo "</lastmod>"; echo "<changefreq>hourly</changefreq>"; echo "<priority>0.8</priority>";echo "</url>";// events.php q=pro "All Current Events by Promoter"$db = mysql_connect('localhost', 'user', 'password');mysql_select_db('database', $db);$result = mysql_query("SELECT * FROM promoters ORDER BY promoter_id", $db);while ($p = mysql_fetch_array($result)) { echo "<url>"; echo "<loc>http://www.ticketstone.com/events.php?q=pro&v="; echo $p['promoter_id']; echo "</loc>"; $time = date('Y-m-d\TH:i:s-05:00', strtotime($p['promoter_mod'])); echo "<lastmod>" . $time . "</lastmod>"; echo "<changefreq>hourly</changefreq>"; echo "<priority>0.3</priority>"; echo "</url>";}echo "</urlset>";?>[/code]promoter_mod is a timestamp column in my table so that I always have the last update time. So for the first record (promoter_id "1000"), the promoter_mod is "20061120071925". When I use the strtotime($p['promoter_mod']), I get a result of -1, which then of course gives me the infamous 69 date when I try to format it with date(). Can anyone help me with the code to be able to take the timestamp in mysql and turn it into "Y-m-d\TH:i:s-05:00"? Puhleese?Chris Quote Link to comment https://forums.phpfreaks.com/topic/27887-mysql-timestamp-not-working-with-php-strtotime/ Share on other sites More sharing options...
craygo Posted November 20, 2006 Share Posted November 20, 2006 you would have to break up the stamp[code]<?php$hr = substr($p['promoter_mod'], 8, 2);$min = substr($p['promoter_mod'],10,2);$sec = substr($p['promoter_mod'],12,2);$mon = substr($p['promoter_mod'],4,2);$day = substr($p['promoter_mod'],6,2);$year = substr($p['promoter_mod'],0,4);$timestamp = mktime($hr, $min, $sec, $mon, $day, $year);$time = date('Y-m-d\TH:i:s-05:00', $timestamp);?>[/code]Ray Quote Link to comment https://forums.phpfreaks.com/topic/27887-mysql-timestamp-not-working-with-php-strtotime/#findComment-127517 Share on other sites More sharing options...
junglyboi Posted November 20, 2006 Author Share Posted November 20, 2006 sweet... works perfectly :D thaaaanks :D Quote Link to comment https://forums.phpfreaks.com/topic/27887-mysql-timestamp-not-working-with-php-strtotime/#findComment-127533 Share on other sites More sharing options...
roopurt18 Posted November 20, 2006 Share Posted November 20, 2006 I find that it's much easier to let the database handle this type of thing for you:[code]<?php$sql = "SELECT *, " . "DATE_FORMAT(promoter_mod, '%Y-%m-%d\t%T') AS promoter_mod_disp " . "FROM promoters ORDER BY promoter_id";$result = mysql_query($sql);?>[/code] Quote Link to comment https://forums.phpfreaks.com/topic/27887-mysql-timestamp-not-working-with-php-strtotime/#findComment-127541 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.