我試圖在保存數據庫中的記錄後4小時後用cron執行php腳本。我的問題不在於cron,所有在這裏都可以正常工作(我確信這是因爲我每4個小時就會收到來自守護進程cron的測試郵件)。爲什麼時間戳unix time和time()不正確之間的區別?
問題是當我計算時間戳,轉換爲unix和當前時間之間的差異,然後執行一些腳本。
但是,這兩個時間差異是他們增加了3個小時,我不知道爲什麼,但我的腳本執行7小時後,而不是4小時。任何人都可以幫助我嗎?
我的代碼如下所示:
<?php
require_once('some-path/wp-load.php');
global $wpdb;
$constant= 4*3600;
$table = 'notifications';
$data = $wpdb->get_results("SELECT * FROM $table WHERE status = 'pending'");
$time = time();
foreach ($data as $r){
$temp_data = strtotime($r->date_created);
if ($time - $temp_data > $constant){
$email = $r->email;
$message = 'test';
$subject = 'test';
$headers = 'From: test <[email protected]>' . "\r\n";
$headers .= 'Content-type: text/html; charset=utf-8' . "\r\n";
$mail_client = wp_mail($email, $subject, $message, $headers);
if($mail_client){
$wpdb->update($tabel, array('status' => 'sent'), array('id_raport'=>$r->id_raport), array('%s'), array('%d'));
}
}
}
從數據庫Using strtotime(): 1458814621, Directly from database "2016-03-24 10:17:01"
現在最新的例子,我知道我可以做這樣的事情來獲得真正的本地時間:
date_default_timezone_set('Europe/Bucharest');
$date = date('m/d/Y h:i:s a', time());
$time = strtotime($date);
更新。 但是這會返回與time()方法相同的結果。
你能否提供數據庫返回的date_created的幾個版本? –
您的時區距UTC有3小時嗎? –
@LiamSorsby,我在問題的結尾添加了你問的內容。希望能幫助到你。謝謝。 – Ionut