php获取年月
㈠ php中获取日期的语句是什么呢
用mktime()函数 返回一个日期的 Unix 时间戳 ,存入数据库,字段用int类型就行了
获取存入数据库时间用,date('Y-m-d H:i:s',$time)就OK了
$time就是查询出来的存入时间字段
㈡ php获取当前时间
PHP获取当前时间可以使用time函数,函数格式为 int time ( void ),返回自从 Unix 纪元(格林威治时间 1970 年 1 月 1 日 00:00:00)到当前时间的秒数。
把time格式的时间以年月日时分秒的格式输出,可以使用date函数,格式是string date ( string $format [, int $timestamp ] ),例子代码:
<?php
$t=time();
echo"$t ";
echodate('Y-m-dH:i:s',$t)
?>
运行结果为:
E:TEMP文件exp>a.php
1451271607
2015-12-2811:00:07
date函数的格式化字符许多,主要的有下面这些:
年:
L 是否为闰年 如果是闰年为 1,否则为 0
o ISO-8601 格式年份数字。这和 Y 的值相同,只除了如果 ISO 的星期数(W)属于前一年或下一年,则用那一年。(PHP 5.1.0 新加) Examples: 1999 or 2003
Y 4 位数字完整表示的年份 例如:1999 或 2003
y 2 位数字表示的年份 例如:99 或 03
月:
F 月份,完整的文本格式,例如 January 或者 March January 到 December
m 数字表示的月份,有前导零 01 到 12
M 三个字母缩写表示的月份 Jan 到 Dec
n 数字表示的月份,没有前导零 1 到 12
t 给定月份所应有的天数 28 到 31
日:
d 月份中的第几天,有前导零的 2 位数字 01 到 31
D 星期中的第几天,文本表示,3 个字母 Mon 到 Sun
j 月份中的第几天,没有前导零 1 到 31
l(“L”的小写字母) 星期几,完整的文本格式 Sunday 到 Saturday
N ISO-8601 格式数字表示的星期中的第几天(PHP 5.1.0 新加) 1(表示星期一)到 7(表示星期天)
S 每月天数后面的英文后缀,2 个字符 st,nd,rd或者 th。可以和 j 一起用
w 星期中的第几天,数字表示 0(表示星期天)到 6(表示星期六)
z 年份中的第几天 0 到 366
星期:
W ISO-8601 格式年份中的第几周,每周从星期一开始(PHP 4.1.0 新加的) 例如:42(当年的第 42 周)
时间:
a 小写的上午和下午值 am 或 pm
A 大写的上午和下午值 AM 或 PM
B Swatch Internet 标准时 000 到 999
g 小时,12 小时格式,没有前导零 1 到 12
G 小时,24 小时格式,没有前导零 0 到 23
h 小时,12 小时格式,有前导零 01 到 12
H 小时,24 小时格式,有前导零 00 到 23
i 有前导零的分钟数 00 到 59>
s 秒数,有前导零 00 到 59>
㈢ php 怎么 获取 整年月份
使用函式 date() 实现
第一种:简单(供学习用)
<?PHP
$today=date("Y-m-dG:i:s");
echo"<center>$today</center>";
?>
第二种:
/*
FormatTime
*/
FunctionformatTime($time,$type="1"){
switch($type){
case1;#2002-06-0418:58Tuesday
returndate("Y.m.dH:i",$time)."<fontcolor=blue>".date("l",$time)."</font>";
case2;#June2002
returndate("MY",$time);
case3;#2002-06-0418:58
returndate("Y.m.dH:i",$time);
case4;#06-04AM
returndate("m-dA",$time);
case5;#06-0418:58
returndate("m.dH:i",$time);
}
}
㈣ php根据年月获取当月天数及日期数组的方法
本文实例讲述了php根据年月获取当月天数及日期数组的方法。分享给大家供大家参考,具体如下:
function
get_day(
$date
)
{
$tem
=
explode('-'
,
$date);
//切割日期
得到年份和月份
$year
=
$tem['0'];
$month
=
$tem['1'];
if(
in_array($month
,
array(
1
,
3
,
5
,
7
,
8
,
01
,
03
,
05
,
07
,
08
,
10
,
12)))
{
//
$text
=
$year.'年的'.$month.'月有31天';
$text
=
'31';
}
elseif(
$month
==
2
)
{
if
(
$year%400
==
0
||
($year%4
==
0
&&
$year%100
!==
0)
)
//判断是否是闰年
{
//
$text
=
$year.'年的'.$month.'月有29天';
$text
=
'29';
}
else{
//
$text
=
$year.'年的'.$month.'月有28天';
$text
=
'28';
}
}
else{
//
$text
=
$year.'年的'.$month.'月有30天';
$text
=
'30';
}
return
$text;
}
echo
get_day('2016-8-1');
运行结果为:31
改造,返回日期数组:
/**
*
获取当月天数
*
@param
$date
*
@param
$rtype
1天数
2具体日期数组
*
@return
*/
function
get_day(
$date
,$rtype
=
'1')
{
$tem
=
explode('-'
,
$date);
//切割日期
得到年份和月份
$year
=
$tem['0'];
$month
=
$tem['1'];
if(
in_array($month
,
array(
1
,
3
,
5
,
7
,
8
,
01
,
03
,
05
,
07
,
08
,
10
,
12)))
{
//
$text
=
$year.'年的'.$month.'月有31天';
$text
=
'31';
}
elseif(
$month
==
2
)
{
if
(
$year%400
==
0
||
($year%4
==
0
&&
$year%100
!==
0)
)
//判断是否是闰年
{
//
$text
=
$year.'年的'.$month.'月有29天';
$text
=
'29';
}
else{
//
$text
=
$year.'年的'.$month.'月有28天';
$text
=
'28';
}
}
else{
//
$text
=
$year.'年的'.$month.'月有30天';
$text
=
'30';
}
if
($rtype
==
'2')
{
for
($i
=
1;
$i
<=
$text
;
$i
++
)
{
$r[]
=
$year."-".$month."-".$i;
}
}
else
{
$r
=
$text;
}
return
$r;
}
var_mp(get_day('2016-8-1','2'));
运行结果如下:
array(31)
{
[0]=>
string(8)
"2016-8-1"
[1]=>
string(8)
"2016-8-2"
[2]=>
string(8)
"2016-8-3"
[3]=>
string(8)
"2016-8-4"
[4]=>
string(8)
"2016-8-5"
[5]=>
string(8)
"2016-8-6"
[6]=>
string(8)
"2016-8-7"
[7]=>
string(8)
"2016-8-8"
[8]=>
string(8)
"2016-8-9"
[9]=>
string(9)
"2016-8-10"
[10]=>
string(9)
"2016-8-11"
[11]=>
string(9)
"2016-8-12"
[12]=>
string(9)
"2016-8-13"
[13]=>
string(9)
"2016-8-14"
[14]=>
string(9)
"2016-8-15"
[15]=>
string(9)
"2016-8-16"
[16]=>
string(9)
"2016-8-17"
[17]=>
string(9)
"2016-8-18"
[18]=>
string(9)
"2016-8-19"
[19]=>
string(9)
"2016-8-20"
[20]=>
string(9)
"2016-8-21"
[21]=>
string(9)
"2016-8-22"
[22]=>
string(9)
"2016-8-23"
[23]=>
string(9)
"2016-8-24"
[24]=>
string(9)
"2016-8-25"
[25]=>
string(9)
"2016-8-26"
[26]=>
string(9)
"2016-8-27"
[27]=>
string(9)
"2016-8-28"
[28]=>
string(9)
"2016-8-29"
[29]=>
string(9)
"2016-8-30"
[30]=>
string(9)
"2016-8-31"
}
更多关于PHP相关内容感兴趣的读者可查看本站专题:《php日期与时间用法总结》、《PHP数组(Array)操作技巧大全》、《PHP基本语法入门教程》、《PHP运算与运算符用法总结》、《php面向对象程序设计入门教程》、《PHP网络编程技巧总结》、《php字符串(string)用法总结》、《php+mysql数据库操作入门教程》及《php常见数据库操作技巧汇总》
希望本文所述对大家PHP程序设计有所帮助。
㈤ 怎么用php获得时间,然后存入mysql,mysql的时间类型是date
需要准备的材料分别是:电脑、php编辑器、浏览器。
1、首先打开mysql查询器,例如以c1表add_time字段为例。
㈥ 请问在PHP中取当前日期的时间戳(年-月-日)该怎么写
用date函数格式化一下就好了!
echodate("Y-m-d",time());
格式化字符串说明见图片~
希望我的回答对你有所帮助!呵呵~
㈦ PHP中用日期函数显示当前年月日喝当前时间
1、格式化输出时间
echo date("Y-m-d H:i:s",time()); //格式化输出时间
//第二个时间是一个时间戳
echo date("Y-m-d H:i:s",0); // 0和负数 返回的是 格林尼治时间元年。
echo date("Y年m月d日 H:i:s",0);
echo date(“n”,time()); //月
echo date(“j”,time()); //天
echo date("h",time()); //时
echo date("w",time()); //星期几
echo date("A",time()); //A表示上下午 , AM——上午 PM——下午
echo date("a",time()); //a表示 上下午 , am——上午,pm——下午。
Y 四位的年
m 月
d 日
H 时
i 分
s 秒
n 月
j 天
h 时
w 星期
㈧ php怎样获取日期中的月份
示例代码如下:
<?php
//日期
$date="2016-11-1111:11:11";
//转换成时间戳
$timestrap=strtotime($date);
//格式化,取出月份
echodate('m',$timestrap);
㈨ php中如何获得当前时间
一、使用函式 date() 实现
在编辑器中输入<?php echo $showtime=date("Y-m-d H:i:s");?>,点击回车就可以得知当前的时间。其中Y是代表4位的年份,H是24小时制,i 是分钟,如: "00" 至 "59" 。s -是秒,如: "00" 至 "59" 。
d 是几日,二位数字,若不足二位则前面补零。 如: "01" 至 "31" 。m代表月份,二位数字,若不足二位则在前面补零,如: "01" 至 "12" 。
二、使用time函数
在编辑器中输入echo date("y-m-d",$time)点击回车就可以得知当前的时间,其中Y是代表4位的年份,m代表月份,二位数字,若不足二位则在前面补零,如: "01" 至 "12" 。d 是几日,二位数字,若不足二位则前面补零。 如: "01" 至 "31" 。
三、使用strftime函数
在编辑器中输入echo strftime ("%hh%m %a %d %b" ,time());点击回车就可以得知当前的时间。
(9)php获取年月扩展阅读:
Date/Time 函数
一、time — 返回当前的 Unix 时间戳
二、timezone_abbreviations_list — 别名 DateTimeZone::listAbbreviations
三、timezone_identifiers_list — 别名 DateTimeZone::listIdentifiers
四、timezone_location_get — 别名 DateTimeZone::getLocation
五、date — 格式化一个本地时间/日期
六、getdate — 取得日期/时间信息
七、gettimeofday — 取得当前时间
八、gmdate — 格式化一个 GMT/UTC 日期/时间
九、gmmktime — 取得 GMT 日期的 UNIX 时间戳
㈩ php从一串字符串中用正则提取出年月日
d{4}/d{1,2}/d{1,2}
如果你不是要求很严谨去判断日期的正确性的话,上面这个就足够了.