php字串轉為日期格式,日期格式轉為字串

2021-09-26 03:42:59 字數 2616 閱讀 6505

strtotime() 函式將任何字串的日期時間描述解析為 unix 時間戳(自 january 1 1970 00:00:00 gmt 起的秒數)。

列印當前時間 php_eol 換行符,相容不同系統

// 將指定日期轉成時間戳,例如"20190813121721"

echo(strtotime(「20190813121721」));

// 將指定日期轉成時間戳,例如"2019-08-13 12:17:21"

echo(strtotime(「2019-08-13 12:17:21」));

//字串轉datetime時間型別 ,例如字串"20190813121721" =>「2019-08-13 12:17:21」

$dt=date(『y-m-d h:i:s』, strtotime(「20190813121721」)).php_eol;

echo($dt);

//時間型別轉為字串型別

s tr

date

=str

toti

me

(strdate=strtotime(

strdat

e=st

rtot

ime(

dt);

echo($strdate);

//將時間轉換成日期字元yyyymmddhhiiss,再轉換成整型格式

d t2

=int

val(

date

(′ym

dhis

′,

dt2 = intval(date('ymdhis',

dt2=in

tval

(dat

e(′y

mdhi

s′,strdate));

echo($dt2);

//previous和next關鍵字=>previous關鍵字可以與星期,天組合使用,表示指定時間的前乙個星期幾或前一天。如下所示**:

echo date(「y-m-d h:i:s」, strtotime(「previous sunday」, strtotime(「2019-08-13」))),php_eol;

//last關鍵字

echo date(「y-m-d h:i:s」, strtotime(「last day」, strtotime(「2019-08-13」))),php_eol;

//back和front關鍵字=>這兩個關鍵字是對一天中的小時的向前和向後操作

//back表示將時間設定指定小時值的後乙個小時的15分的位置。如果是24點,則算到第二天的0點15分。

//front表示將時間設定指定小時值的前乙個小時的45分的位置。如果是0點,則算前一天的23點45分。

//其中back of和front of後接的陣列必須大於等於0並且小於等於24。

//結果:2019-08-14 00:15:00 2019-08-13 23:45:001516234088

echo date(「y-m-d h:i:s」,strtotime(「back of 24」,strtotime(「2019-08-13」))),php_eol;

echo date(「y-m-d h:i:s」,strtotime(「front of 24」,strtotime(「2019-08-13」))),php_eol;

// 更多例項

echo date(『y-m-d h:i:s』, strtotime(『2019-08-13 12:17:21』)).php_eol;

echo date(『y-m-d h:i:s』, strtotime(『20190813121721』)).php_eol;

//昨天

echo date(『y-m-d h:i:s』, strtotime(『yesterday』)).php_eol;

//上週

echo date(『y-m-d h:i:s』, strtotime(『last week』)).php_eol;

//本週開始時間

echo date(『y-m-d h:i:s』, strtotime(『this week midnight』)).php_eol;

//本月開始時間

echo date(『y-m-d h:i:s』, strtotime(『first day of this month midnight』)).php_eol;

//計算相對時間

echo date(『y-m-d h:i:s』, strtotime(』+1 month』)).php_eol;

echo strtotime(「now」),php_eol;

echo strtotime(「now」),php_eol;

echo strtotime(「10 september 2000」), php_eol;

echo strtotime("+1 day"), php_eol;

echo strtotime("+1 week"), php_eol;

echo strtotime("+1 week 2 days 4 hours 2 seconds"), php_eol;

echo strtotime(「next thursday」), php_eol;

echo strtotime(「last monday」), php_eol;

php 字串格式化為日期時間 php時間格式化

用法 date 格式,時間 如果沒有時間引數,則使用當前時間.格式是乙個字串,其中以下字元有特殊意義 u 替換成從乙個起始時間1970年1月1日以來的秒數 echo 時間格式1 date y m d h i s 2010 06 12 10 26 31 echo 時間格式2 date y m d h ...

日期的格式 字串轉日期

var mydate new date 輸出 sat nov 26 2016 16 15 27 gmt 0800 中國標準時間 mydate.tolocaledatestring 可以獲取當前日期 下午4 15 27 mydate.tolocaletimestring 可以獲取當前時間 2016 1...

日期格式字串詳解

今天做專案遇到日期轉換問題,特收集一下相關資料。日期格式字串 yyyy mm dd hh mm ss 詳解 d月份中的日。單數字的日不會有前導符零。dd月份中的日。單數字的日將會有乙個前導符零。ddd星期的縮寫名稱,如 abbreviateddaynames 中所定義的。dddd星期的完整名稱,如 ...