2012-03-02 24 views
1

我希望你們有時間讓一隻老狗(71歲)學習新的技巧。我有大約一週的PHP和MySQL經驗,但我有一些樂趣,學習它如何工作,我已經取得了一些進展。但是,我有這個查詢/邏輯/語法問題,如果你不嘲笑我的拗口,我想獲得一些意見。查詢sql表中的時間不正確由於位數不同

我有一個日曆程序,不能以我想要的方式發出提醒。我以爲我會想辦法解決它的樂趣,但我不能改變表格的內容,以免我整個程序被垃圾破壞。我成功地做了我想做的事情,就是通過只收取「時間」表條目並對它們進行排序並將它們過濾到我想要的內容,從而在一封電子郵件中發送我所有的提醒。但幾天後,我注意到,在我17:00之前(格林威治標準時間-7)之前的任何條目都沒問題,但之後的任何條目都是垃圾。所以仔細觀察桌面,我注意到格林威治標準時間23點59分之後的任何時間都是5位數字,而之前的任何時間都是6位數字。所以我發現了這個問題。我認爲我所需要做的就是在中間放置一個IF並相應地添加額外的數字,並將其更改爲UNIX紀元時間,然後執行date()並僅使用小時和分鐘。你笑嗎?讓我重申一下,我對PHP和MySQL知之甚少,但我正在學習,這讓我開心,也許會更年輕。

這是我得到的。我只會發送查詢,因爲掛鉤到數據庫和發送郵件工作正常。這第一個作品......除了之前提到的問題...

date_default_timezone_set('America/Denver'); 
$today = date("Ymd"); 
mysql_select_db($dbname); 
$query = sprintf("SELECT cal_name, cal_time, cal_description, cal_date FROM webcal_entry WHERE cal_date = '".$today."'"); 
$result = mysql_query($query); 
$body = ''; 
$mail = ''; 
while ($row = mysql_fetch_array($result, MYSQL_NUM)) { 
    $mail = sprintf("Event: %s \nTime: %s \nDesc: %s \n\n", $row[0], date("H:i",((strtotime($row[1])-25200))), $row[2]); 
} 
mysql_free_result($result); 

這是瘋狂的修復是真的失去了一些東西......

date_default_timezone_set('America/Denver'); 
$today = date("Ymd"); 
mysql_select_db($dbname); 
$query = sprintf("SELECT cal_name, cal_time, cal_description, cal_date FROM webcal_entry WHERE cal_date = '".$today."'"); 
$result = mysql_query($query); 
$body = ''; 
$mail = ''; 
$stt = ''; 
while ($row = mysql_fetch_array($result, MYSQL_NUM)) { 
    $mail = sprintf("Event: %s \nTime: %s \nDesc: %s \n\n", $row[0], ($stt = { if ($row[1] < 100000, + 1234500000) ELSE ($row[1] + 1234000000) }) date("H:i",((strtotime($stt)-25200))), $row[2]) 
}; 

我希望你們中的一個希望把這個和幽默的老傢伙...停下來笑!

+0

類型爲'DATE'和'TIME'的'cal_date'和'cal_time'列? – Chris 2012-03-02 05:56:37

+0

我相信答案是否定的。例如,cal_date表示日期爲20120301,時間表示爲5或6位數字,即。 01:30 = 13000和13:00 = 130000.如果你是表中的類型,它們都是int(11)。 – defacto7 2012-03-02 07:00:20

回答

1

請嘗試以下操作。我添加了註釋,便於理解:包含時間和日期值

/* I assume that the times and dates in your database are stored in UTC, 
    so set the UTC timezone first. */ 
date_default_timezone_set('UTC'); 

/* $today might now contain a value of tomorrow if it's past 17:00 in your timezone. 
    This is required though, as there could be an event at e.g. 18:00 of your time which would 
    not be returned otherwise. sprintf is not needed, as you don't format anyting here */ 
$today = date("Ymd"); 
$query = "SELECT cal_name, cal_time, cal_description, cal_date FROM webcal_entry WHERE cal_date = " . $today; 

mysql_select_db($dbname); 
$result = mysql_query($query); 

$body = ''; 
$mail = ''; 

while ($row = mysql_fetch_array($result, MYSQL_NUM)) { 
    // Store the value of the returned time in a $fixedTime 
    $fixedTime = $row[1]; 

    /* Simple approach: While the length of the time is less than 6 digits, prepend a leading 0 
     This will even work for times like 00:00:15 */ 
    while (strlen($fixedTime) < 6) 
     $fixedTime = '0' . $fixedTime; 

    // Now store the unix timestamp (still in UTC!) 
    $unixEpoch = strtotime($fixedTime); 

    // Set the timezone to your local time ... 
    date_default_timezone_set('America/Denver'); 

    // ... and $formattedTime receives the formatted time value in your timezone 
    $formattedTime = date("H:i", $unixEpoch); 

    // Now create the mail content 
$mail .= sprintf("Event: %s \nTime: %s \nDesc: %s \n\n", $row[0], $formattedTime, $row[2]); 
} 

mysql_free_result($result); 

數據庫列應該被聲明爲DATETIMEDATETIME雖然。
這會讓事情變得更容易,因爲MySQL可以處理時區轉換和日期/時間格式。

+0

哈...像夢一樣工作!非常感謝你。時代決定了我需要他們的方式,但最大的幫助是我拿起了關於如何使用php/sql的幾點指示。現在我在這個劇本中做了一個大的跳躍,但我會自己研究一段時間,並應用一些你教給我的校長。如果我撞牆,我會回來,但我想把它放在最好的位置。 *自我 - >現在我怎麼給+票? * – defacto7 2012-03-02 18:52:15

+0

@ defacto7:我很高興能幫上忙。您無法提供+票(upvotes),因爲您尚未獲得15點聲望點。但是,如果你想:-),你可以接受答案。請參閱[如何接受答案](http://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work) – Chris 2012-03-02 18:57:22