PHP時間戳記與日期之間轉換

來源:互聯網
上載者:User

strtotime()函數 strtotime() 函數將任何英文文本的日期時間描述解析為 Unix 時間戳記。


例子

 代碼如下 複製代碼

<?php
echo(strtotime("now"));
echo(strtotime("3 October 2005"));
echo(strtotime("+5 hours"));
echo(strtotime("+1 week"));
echo(strtotime("+1 week 3 days 7 hours 5 seconds"));
echo(strtotime("next Monday"));
echo(strtotime("last Sunday"));
?>

輸出:

1138614504
1128290400
1138632504
1139219304
1139503709
1139180400
1138489200

上面是把日期轉換成時間戳記了,我們也可以把如 2013-04-21這種轉換成時間戳記

 代碼如下 複製代碼

$a = date();
$mk = strtotime($a)

要求只能在白天8:00-20:00傳送簡訊,怎麼樣獲得到每天的這段時間區間?

 代碼如下 複製代碼


<?
$y=date("Y",time());
$m=date("m",time());
$d=date("d",time());
$start_time = mktime(9, 0, 0, $m, $d ,$y);
$end_time = mktime(19, 0, 0, $m, $d ,$y);

$time = time();
if($time >= $start_time && $time <= $end_time)
{
// do something....
}
?>


下面來介紹把時間戳轉換成日期


date()函數,

此函數不但可以擷取各種各樣的時間日期之外,還可以進行日期轉換呼

 代碼如下 複製代碼

<?
$time = time();
$date = date("Y-m-d",$time);
echo 'www.111cn.net 提示'.$date
?>

這樣就輸出 www.111cn.net 提示 2013-04-21 。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.