Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Apr 24th, 2012  |  syntax: None  |  size: 2.50 KB  |  hits: 9  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. query of time in sql table incorrect due to varied number of digits
  2. date_default_timezone_set('America/Denver');
  3. $today = date("Ymd");
  4. mysql_select_db($dbname);
  5. $query = sprintf("SELECT cal_name, cal_time, cal_description, cal_date FROM webcal_entry WHERE cal_date = '".$today."'");
  6. $result = mysql_query($query);
  7. $body = '';
  8. $mail = '';
  9. while ($row = mysql_fetch_array($result, MYSQL_NUM)) {
  10.     $mail = sprintf("Event: %s nTime: %s nDesc: %s nn", $row[0], date("H:i",((strtotime($row[1])-25200))), $row[2]);
  11. }
  12. mysql_free_result($result);
  13.        
  14. date_default_timezone_set('America/Denver');
  15. $today = date("Ymd");
  16. mysql_select_db($dbname);
  17. $query = sprintf("SELECT cal_name, cal_time, cal_description, cal_date FROM webcal_entry WHERE cal_date = '".$today."'");
  18. $result = mysql_query($query);
  19. $body = '';
  20. $mail = '';
  21. $stt = '';
  22. while ($row = mysql_fetch_array($result, MYSQL_NUM)) {
  23.     $mail = sprintf("Event: %s nTime: %s nDesc: %s nn", $row[0], ($stt = { if ($row[1] < 100000, + 1234500000) ELSE ($row[1] + 1234000000) }) date("H:i",((strtotime($stt)-25200))), $row[2])
  24. };
  25.        
  26. /* I assume that the times and dates in your database are stored in UTC,
  27.      so set the UTC timezone first. */
  28. date_default_timezone_set('UTC');
  29.  
  30. /* $today might now contain a value of tomorrow if it's past 17:00 in your timezone.
  31.      This is required though, as there could be an event at e.g. 18:00 of your time which would
  32.      not be returned otherwise. sprintf is not needed, as you don't format anyting here */
  33. $today = date("Ymd");
  34. $query = "SELECT cal_name, cal_time, cal_description, cal_date FROM webcal_entry WHERE cal_date = " . $today;
  35.  
  36. mysql_select_db($dbname);
  37. $result = mysql_query($query);
  38.  
  39. $body = '';
  40. $mail = '';
  41.  
  42. while ($row = mysql_fetch_array($result, MYSQL_NUM)) {
  43.     // Store the value of the returned time in a $fixedTime
  44.     $fixedTime = $row[1];
  45.  
  46.     /* Simple approach: While the length of the time is less than 6 digits, prepend a leading 0
  47.          This will even work for times like 00:00:15 */
  48.     while (strlen($fixedTime) < 6)
  49.         $fixedTime = '0' . $fixedTime;
  50.  
  51.     // Now store the unix timestamp (still in UTC!)
  52.     $unixEpoch = strtotime($fixedTime);
  53.  
  54.     // Set the timezone to your local time ...
  55.     date_default_timezone_set('America/Denver');
  56.  
  57.     // ... and $formattedTime receives the formatted time value in your timezone
  58.     $formattedTime = date("H:i", $unixEpoch);
  59.  
  60.     // Now create the mail content
  61. $mail .= sprintf("Event: %s nTime: %s nDesc: %s nn", $row[0], $formattedTime, $row[2]);
  62. }
  63.  
  64. mysql_free_result($result);