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

答案1:

输出结果:

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:

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 是可选的,默认值为

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 时间戳:

<?phpecho (strtotime("now") . "
");echo(strtotime("15 October 1980") . "
");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"));?>

运行结果:

1488465569

1473004800

1488483569

1489070369

1489354774

1488729600

1488038400

日期转换为时间戳

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

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

mktime():从日期取得时间戳。

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值