[英]php - if date older than 6 months ago
我希望下面的代碼檢查客戶的創建日期,如果他們已經有6個月的客戶了,那么促銷的價格就會上漲。 我嘗試下面的方法時並不高興,我認為它只是使用月份而不考慮年份。 例如,如果客戶在9月上船,則第二個月是6個月,並且永遠不會改變。
謝謝
$created_date = date('m',strtotime($customer_order['created_at']));
$current = date("m");
$curmonth = date("m");
$ordermonth = date("m",strtotime($udata['created_date']));
$m_dff = $curmonth - $ordermonth;
//print_r($m_dff."<br>");
if($m_dff > 6){
$unitcost = 19.99;
}
else{
$unitcost = 14.99;
}
比您的示例可以更有效地使用strtotime()
,以下方法可以解決問題
if(time() > strtotime($customer_order['created_at'] . ' +6 months')) {
$unitcost = 19.99;
} else {
$unitcost = 14.99;
}
$currentorder = date ('Y-m-d');
$createdaccount = date('Y-m-d', strtotime("+6 months",
strtotime($customer_order['created_at'])));
if($currentorder>=$createdaccount)
{
$unitcost = 19.99;
}
else
{
$unitcost = 14.99;
}
查看使用DateTime對象進行比較是否可行。
//set a general time zone at top of script
date_default_timezone_set('Europe/Amsterdam');
// if created_at is a valid dateTime string
$created = new \DateTime($customer_order['created_at']);
$current = new \DateTime();
$sixMonth = $created->add(new \DateInterval(‘P6M’));
if ($created < $current) {
// if created + 6 months is older than current
$price = 19.99;
} else {
$price = 14.99;
}
有關更多信息,請參見: https : //secure.php.net/manual/en/book.datetime.php
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.