2013-09-27 24 views
3

我有問題,當我想計算日期。簡單的例子:php如何做日期操作

我有2013-09-01是開始日期,我有每月30天。我的工作我需要在月底前10天向我的用戶發出警報(這意味着我需要提醒消息it's 10day more for end of this month)。所以每個人都有任何想法來幫助計算它。因爲我不喜歡(+, -, *,/)。現在我有一些數據像

<?php 
    date_default_timezone_set('Asia/Phnom_Penh'); 
    $current = time(); 
    $start = 1380188957; 
    echo 'Start date: '. date('Y-m-d', $start) ."\n"; 
    echo '<br/>'; 
    $repeat = 30; 
    $enddate = time() + ($repeat * 24 * 60 * 60); 
    echo 'end date: '. date('Y-m-d', $enddate) ."\n"; 

感謝在出現的幫助。

+0

對於我的理念:我想聲明'$ current'到'和'sub' $ start'如果它是較小的或Equuleus(<=)'20,我會提醒消息 – koe

+0

嘿時間。任何答案是否能解決您的問題?如果是的話,你可以接受其中之一。 – Jeemusu

回答

0
$start = 1380188957; 
$enddate = time() + ($repeat * 24 * 60 * 60); 

這是你自己的代碼。使用,我們可以很容易地計算10 days before end date

$alert=$enddate-864000; // That's 10 days 
$alertdate=date('Y-m-d', $alert); 
+0

非常感謝你,但我需要動態的不想知道只有靜態那樣,你能告訴我如何計算'4400' ='10day'嗎? – koe

+1

1天='24小時x 60分鐘x 60秒'= 864000.現在,如果您必須計算6天,只需乘以6即可得出答案。但有趣的是,它已經是你的代碼的一部分,看到'$重複'行?那是做同樣的事情。抱歉,我的號碼錯了,缺少分鐘。只是更新了 –

+0

是的我有步驟測試'10天,6天和...'感謝您的幫助,我現在會測試它 – koe

2

不是每個月有31天,您可以通過使用在PHP的date()函數的字符串格式PARAM的t選項得到任何一個月的天數。

// Current time as unix timestamp 
$now = time(); 

// Number of days in current month 
$days_this_month = date("t", time()); 

// Last day of the current month as a unix timestamp; 
$end_of_month = strtotime(date("Y-m-t", time())); 

// Ten days before the end of the month as a unix timestamp 
$ten_days = strtotime('-10 days', $end_of_month); 

現在我們可以做一個檢查,看它是否是在月底前10天:

if($now > $ten_days) { 

    // Do something 
} 
+0

非常好的答案。 +1 –