码迷,mamicode.com
首页 > Web开发 > 详细

用PHP打印出前一天的时间,打印格式是2007年5月10日22:21:21

时间:2017-03-02 23:53:27      阅读:541      评论:0      收藏:0      [点我收藏+]

标签:echo   function   ons   set   date   selected   打印   mysq   test   

答案1:

<?php
echo date(‘Y‘.‘年‘.‘m‘.‘月‘.‘d‘.‘日‘.‘ H:i:s‘,strtotime(‘-1 day‘));

输出结果:

Warning: strtotime(): It is not safe to rely on the system‘s timezone settings. You are *required* to use the date.timezone setting or the date_default_timezone_set() function. In case you used any of those methods and you are still getting this warning, you most likely misspelled the timezone identifier. We selected the timezone ‘UTC‘ for now, but please set date.timezone to select your timezone. in C:\AppServ\www\test2.php on line 2

Warning: date(): It is not safe to rely on the system‘s timezone settings. You are *required* to use the date.timezone setting or the date_default_timezone_set() function. In case you used any of those methods and you are still getting this warning, you most likely misspelled the timezone identifier. We selected the timezone ‘UTC‘ for now, but please set date.timezone to select your timezone. in C:\AppServ\www\test2.php on line 2
2017-03-01 15:06:41

 

答案2:

<?php
date_default_timezone_set(‘UTC‘);
echo date(‘Y‘.‘年‘.‘m‘.‘月‘.‘d‘.‘日‘.‘ H:i:s‘,strtotime(‘-1 day‘));

运行结果:

2017年03月01日 15:06:43

date() — 格式化一个本地时间/日期

string date ( string $format [, int $timestamp ] )

返回将整数 timestamp 按照给定的格式字串而产生的字符串。如果没有给出时间戳则使用本地当前时间。换句话说,timestamp 是可选的,默认值为 time()

Note:要将字符串表达的时间转换成时间戳,应该使用 strtotime()。此外一些数据库有一些函数将其时间格式转换成时间戳(例如 MySQL 的 ? UNIX_TIMESTAMP 函数)。

 

strtotime() — 将任何字符串的日期时间描述解析为 Unix 时间戳

 定义和用法

 strtotime() 函数将任何英文文本的日期或时间描述解析为 Unix 时间戳(自 January 1 1970 00:00:00 GMT 起的秒数)。 

int strtotime ( string $time [, int $now = time() ] )

 参数

time:日期/时间字符串。正确格式的说明详见 日期与时间格式
now:用来计算返回值的时间戳。如果省略该参数,则使用当前时间。

返回值

成功则返回时间戳,否则返回 FALSE。在 PHP 5.1.0 之前本函数在失败时返回 -1

实例

将英文文本日期时间解析为 Unix 时间戳:

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

运行结果:

1488465569
1473004800
1488483569
1489070369
1489354774
1488729600
1488038400 

http://www.w3school.com.cn/php/func_date_strtotime.asp

http://php.net/manual/zh/function.strtotime.php

mktime — 取得一个日期的 Unix 时间戳

日期转换为时间戳

PHP 提供了函数可以方便的将各种形式的日期转换为时间戳,该类函数主要是:

  • strtotime():将任何英文文本的日期时间描述解析为时间戳。
  • mktime():从日期取得时间戳。

 

用PHP打印出前一天的时间,打印格式是2007年5月10日22:21:21

标签:echo   function   ons   set   date   selected   打印   mysq   test   

原文地址:http://www.cnblogs.com/gengyi/p/6493014.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!