国产三级农村妇女在线,国产精品毛片a∨一区二区三区,国产乱子伦视频大全,国产精品色拉拉,国产欧美日韩一区二区三区,

首頁(yè) > 技術(shù) > IOS開(kāi)發(fā)

php中日期類型轉(zhuǎn)換實(shí)例講解

IOS開(kāi)發(fā) 2023-02-16 22:21:11

1、使用date()函數(shù)將UNIX時(shí)間戳轉(zhuǎn)換為日期。

2、使用strtotime()函數(shù)將日期轉(zhuǎn)換為UNIX時(shí)間戳。

在PHP中是可以完成日期格式轉(zhuǎn)換的,不過(guò)有一個(gè)缺點(diǎn)就是占用PHP解析器的解析時(shí)間,因此速度會(huì)相對(duì)慢一些。但是這種方式也有優(yōu)點(diǎn),那就是不管是不是數(shù)據(jù)庫(kù)中查詢獲得的數(shù)據(jù)都可以進(jìn)行轉(zhuǎn)換,轉(zhuǎn)換范圍不受限制。

實(shí)例

$y=date("Y",time());         //年
$m=date("m",time());      //月
$d=date("d",time());        //日
echo $y."
";
echo $m."
";
echo $d."
";
$eight_clock = mktime(8, 0, 0, $m, $d ,$y);  //每天8點(diǎn)
echo date("Y-m-d H:i:s",$eight_clock)."
";
$day_time = mktime(0, 0, 0, $m, 1 ,$y);      //每月1號(hào)
echo date("Y-m-d H:i:s",$day_time)."
";

實(shí)例擴(kuò)展:

// convert a date into a string that tells how long ago
// that date was.... eg: 2 days ago, 3 minutes ago.
function ago($d) {
 $c = getdate();
 $p = array('year', 'mon', 'mday', 'hours', 'minutes', 'seconds');
 $display = array('year', 'month', 'day', 'hour', 'minute', 'second');
 $factor = array(0, 12, 30, 24, 60, 60);
 $d = datetoarr($d);
 for ($w = 0; $w < 6; $w++) {
 if ($w > 0) {
  $c[$p[$w]] += $c[$p[$w-1]] * $factor[$w];
  $d[$p[$w]] += $d[$p[$w-1]] * $factor[$w];
 }
 if ($c[$p[$w]] - $d[$p[$w]] > 1) { 
  return ($c[$p[$w]] - $d[$p[$w]]).' '.$display[$w].'s ago';
 }
 }
 return '';
}
// you can replace this if need be. 
// This converts my dates returned from a mysql date string 
// into an array object similar to that returned by getdate().
function datetoarr($d) {
 preg_match("/([0-9]{4})(-)([0-9]{2})(-)([0-9]{2})([0-9]{2})(:)([0-9]{2})(:)([0-9]{2})/",$d,$matches);
 return array( 
 'seconds' => $matches[10], 
 'minutes' => $matches[8], 
 'hours' => $matches[6], 
 'mday' => $matches[5], 
 'mon' => $matches[3], 
 'year' => $matches[1], 
 );
}

到此這篇關(guān)于php中日期類型轉(zhuǎn)換實(shí)例講解的文章就介紹到這了,更多相關(guān)php中日期類型的轉(zhuǎn)換內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

TAg

加載中~

本網(wǎng)站LOGO受版權(quán)及商標(biāo)保護(hù),版權(quán)登記號(hào):國(guó)作登字-2022-F-10126915,未經(jīng)湖南木星科技官方許可,嚴(yán)禁使用。
Copyright ? 2012-2022 湖南木星科技有限公司(木星網(wǎng))版權(quán)所有
轉(zhuǎn)載內(nèi)容版權(quán)歸作者及來(lái)源網(wǎng)站所有,本站原創(chuàng)內(nèi)容轉(zhuǎn)載請(qǐng)注明來(lái)源,商業(yè)媒體及紙媒請(qǐng)先聯(lián)系:aishangyiwan@126.com

工信部備案號(hào):湘ICP備19012813號(hào)-5