PHP Unix时间缺少零

时间:2016-10-27 01:58:37

标签: php date time

好的,所以我有以下公式,但由于某种原因,我无法判断它是5分钟还是50分钟,因为它不会显示零或零。所以,当我回应“$ h:$ m”;例如,它显示为7:5。有任何想法吗?

    $now = time();
    $diff = $now - $phpdate;
    $w = $diff / 86400 / 7;
    $d = $diff / 86400 % 7;
    $h = $diff / 3600 % 24;
    $m = $diff / 60 % 60; 
    $s = $diff % 60;

    $PD = "$h:$m";

2 个答案:

答案 0 :(得分:0)

为了避免其他开销,因为您需要的只是一个简单的填充,您可以填充需要长度为2的填充(当然,填充为'0',但仅当字符串长度不是两个时):< / p>

Select Store, CUSTID, CUST.ID_CUST,

 Sum(
  CASE  
    WHEN Cust_Gift.Code_Status = 'C' AND Gift_Item.FLAG_STORE_LOC = 'N'
    THEN Cust_Gift.AMT_PAID ELSE 0 
  END) GiftAmt,

 Sum(
  CASE WHEN Cust_Gift.Code_Status = 'C' AND Gift_Item.FLAG_STORE_LOC  = 'Y'
   THEN Cust_Gift.AMT ELSE 0 
  END) CustGiftAmt,

Sum(
  CASE WHEN Cust_Coupon.Code_Status = 'C' 
   THEN Cust_Coupon.AMT
   ELSE 0 
  END) CouponAmt,

Sum(CASE WHEN Cust_Sports.Status = 'C' 
  THEN Cust_Sports.AMT
  ELSE 0 
END) SportsAmt

FROM CUST
  LEFT OUTER JOIN CUST_GIFT
   ON CUST.ID_CUST = CUST_GIFT.ID_CUST
  LEFT OUTER JOIN CUST_COUPON 
   ON CUST.ID_CUST = CUST_COUPON.ID_CUST
  LEFT OUTER JOIN CUST_SPORTS
   ON CUST.ID_CUST = CUST_SPORTS.ID_CUST
  INNER JOIN GIFT_ITEM
   ON CUST_GIFT.ID_GIFT_ITEM = GIFT_ITEM.ID_GIFT_ITEM

WHERE (STORE = 'M669098' OR STORE = 'M66923434' )

Group by CustID, Store, CUST.ID_CUST

答案 1 :(得分:0)

您可以使用str_pad并且可以在小时和分钟前添加0字符串,如果它们是单个数字。你可以这样做 -

$now = time();
$diff = $now - 30939007;
$w = $diff / 86400 / 7;
$d = $diff / 86400 % 7;
$h = $diff / 3600 % 24;
$m = $diff / 60 % 60; 
$s = $diff % 60;

// add 0 to left if string is single chars
if (strlen($h) == 1) {
  $h = str_pad($h, '2', '0', STR_PAD_LEFT);
}
if (strlen($m) == 1) {
 $m = str_pad($m, '2', '0', STR_PAD_LEFT);
}

$PD = "$h:$m";
echo $PD;

Check str_pad documentation

希望这对你有帮助。