首页 后端开发 PHP问题 php怎么获取时间戳

php怎么获取时间戳

Aug 04, 2020 pm 02:07 PM
php 时间戳

php获取时间戳的方法:可以利用time()函数或strtotime()函数来获取。time()函数返回一个包含当前时间的Unix时间戳的整数。strtotime()函数将任何字符串的日期时间描述解析为Unix时间戳。

php怎么获取时间戳

time() 函数返回自 Unix 纪元(January 1 1970 00:00:00 GMT)起的当前时间的秒数。返回一个包含当前时间的 Unix 时间戳的整数。

(推荐教程:php图文教程

代码实现:

<?php
$t=time();
echo($t . "<br>");
echo(date("Y-m-d",$t));
?>

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

语法:

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

(视频教程推荐:php视频教程

参数:

  • time 必需。规定日期/时间字符串。

  • now 可选。规定用来计算返回值的时间戳。如果省略该参数,则使用当前时间。

代码实现:

<?php
// 设置时区
date_default_timezone_set("PRC");
$time = strtotime("2018-01-18 08:08:08");  // 将指定日期转成时间戳 
// 打印当前时间  PHP_EOL 换行符,兼容不同系统
echo  $time, PHP_EOL; 
// 更多实例
echo strtotime("now"), PHP_EOL;e
cho 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中文网其他相关文章!

本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

热AI工具

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Stock Market GPT

Stock Market GPT

人工智能驱动投资研究,做出更明智的决策

热工具

记事本++7.3.1

记事本++7.3.1

好用且免费的代码编辑器

SublimeText3汉化版

SublimeText3汉化版

中文版,非常好用

禅工作室 13.0.1

禅工作室 13.0.1

功能强大的PHP集成开发环境

Dreamweaver CS6

Dreamweaver CS6

视觉化网页开发工具

SublimeText3 Mac版

SublimeText3 Mac版

神级代码编辑软件(SublimeText3)

热门话题

如何检查电子邮件地址在PHP中是否有效? 如何检查电子邮件地址在PHP中是否有效? Sep 21, 2025 am 04:07 AM

usefilter_var()

如何在PHP中制作对象的深度副本或克隆? 如何在PHP中制作对象的深度副本或克隆? Sep 21, 2025 am 12:30 AM

useunSerialize(serialize($ obj))fordeepcopyingwhenalldataiSerializable;否则,exhiment__clone()tomanallyDuplicateNestedObjectedObjectSandAvoidSharedReference。

如何在PHP项目中使用名称空间? 如何在PHP项目中使用名称空间? Sep 21, 2025 am 01:28 AM

NamespacesinPHPorganizecodeandpreventnamingconflictsbygroupingclasses,interfaces,functions,andconstantsunderaspecificname.2.Defineanamespaceusingthenamespacekeywordatthetopofafile,followedbythenamespacename,suchasApp\Controllers.3.Usetheusekeywordtoi

如何在PHP中调用父构建器? 如何在PHP中调用父构建器? Sep 21, 2025 am 03:43 AM

当extervendingAclassInphp时,callparent :: __ construct()toensuretheparent'sinitializationLogicRuns.1.iftheparentClassConstructorSetSupessentialProperties,SkeippingThiscallMayResultInCompleteTeleteTeTeborteTepleteTebignEbigntInitialization.2.useparent.2.useceparent.2.usectectectectecon :: __ _______________________________)insideTheChild’sco

如何使用PHP更新数据库中的记录? 如何使用PHP更新数据库中的记录? Sep 21, 2025 am 04:47 AM

toupdateadatabaseRecordInphp,firstConnectusingpDoormySqli,thenusepreparedStatementStoExecuteAsecuteAsecuresqurupDatequery.example.example:$ pdo = newpdo(“ mySql:mysql:host = localHost; localhost; localhost; dbname; dbname = your_database = your_database',yous_database',$ username,$ username,$ squeaste;

如何查看PHP中的文件权限? 如何查看PHP中的文件权限? Sep 22, 2025 am 06:27 AM

usefileperms()togetFilePermissionsasanIntegerAntegatusingsPrintf('%o')

如何在PHP中实现单身模式? 如何在PHP中实现单身模式? Sep 25, 2025 am 12:27 AM

单例模式确保一个类只有一个实例,并提供全局访问点,适用于需要单一对象协调系统操作的场景,如数据库连接或配置管理。2.其基本结构包括:私有的静态属性存储实例、私有构造函数防止外部创建、私有克隆方法防止复制,以及公共静态方法(如getInstance())用于获取实例。3.在PHP中通过调用getInstance()方法获取唯一实例,无论调用多少次都返回同一对象引用。4.标准PHP请求模型下无需考虑线程安全,但在长运行或多线程环境中需注意同步问题,而PHP本身不支持原生锁机制。5.尽管单例有用,但会

如何在PHP脚本中获取命令行参数? 如何在PHP脚本中获取命令行参数? Sep 22, 2025 am 06:19 AM

使用$argv和$argc获取PHP命令行参数,$argc为参数数量,$argv为参数数组,如phpscript.phphelloworld中$argc=3,$argv=['script.php','hello','world'];用$argv[1]等访问具体参数;复杂场景可用getopt()处理短选项(-f)和长选项(--file)。

See all articles