Trying to define my own time type in PHP(试图在 PHP 中定义我自己的时间类型)
问题描述
所以我试图定义一个时间类型,但我不太确定如何去做.我在网上找到的答案给出了用当前时间定义时间类型的例子(即 date("h:i:sa"))但是我正在尝试定义一个硬编码版本.我的格式想要的是 (HH:mm:ss) (hour:minutes:seconds) 我需要将变量声明为时间类型的原因是为了以后可以使用它们进行比较.
So I am trying to define a time type and I am not quite sure how to do it. The answers I find online give examples of defining a time type with the current time (i.e date("h:i:sa")) however I am trying to define a hard coded version.The format I want is (HH:mm:ss) (hour:minutes:seconds) The reason why I need the variable to be declared as time type is so that they can be used later on to be compared.
<?php
$my_time = '10:00:00';
$your_time = '11:00:00';
if($my_time > $your_time){
echo "You have less time";
}
?>
推荐答案
PHP DateTime 类就是你所需要的.
The PHP DateTime Class is what you need.
//instantiate a DateTime Object
$time = new DateTime();
//Use the DateTime obj to create two new DateTime objects
$yourTime = $time->createFromFormat('h:i:s', '12:30:30'); //third param here can define tz
$theirTime = $time->createFromFormat('h:i:s', '12:10:15');
//Use diff() to return a DateInterval
$dateInterval = $yourTime->diff($theirTime);
//Format the DateInterval as a string
$differenceBetweenYoursAndTheirs = $dateInterval->format('%h:%i:%s');
//Do something with your interval
echo "The difference between {$yourTime->format('h:i:s')} and {$theirTime->format('h:i:s')} is $differenceBetweenYoursAndTheirs";
这篇关于试图在 PHP 中定义我自己的时间类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:试图在 PHP 中定义我自己的时间类型
基础教程推荐
- XAMPP 服务器不加载 CSS 文件 2022-01-01
- 在 PHP 中强制下载文件 - 在 Joomla 框架内 2022-01-01
- 如何在 PHP 中的请求之间持久化对象 2022-01-01
- 通过 PHP SoapClient 请求发送原始 XML 2021-01-01
- 超薄框架REST服务两次获得输出 2022-01-01
- 在多维数组中查找最大值 2021-01-01
- Libpuzzle 索引数百万张图片? 2022-01-01
- mysqli_insert_id 是否有可能在高流量应用程序中返回 2021-01-01
- 在 Woocommerce 中根据运输方式和付款方式添加费用 2021-01-01
- WooCommerce 中选定产品类别的自定义产品价格后缀 2021-01-01