如何在 php 中将字符串时间转换为日期时间格式


how to convert String time to dateTime format in php?

我想检查两个日期时间变量之间的日期时间谁更大。

我有一串日期时间,我想将其转换为日期时间格式并检查它谁更大?

我的变量:

$d1 = '2016-02-02 07:35:00';
$d2 = '2016-02-1 13:10:31';

我想检查上述两个变量之间谁更大。

if($d1>$d2)
{
return true;
}

php 中有 DateTime 类的比较运算符。像这样:

date_default_timezone_set('Europe/London');
$d1 = new DateTime('2008-08-03 14:52:10');
$d2 = new DateTime('2008-01-03 11:11:10');
var_dump($d1 == $d2);
var_dump($d1 > $d2);
var_dump($d1 < $d2);

输出

bool(false)
bool(true)
bool(false)

使用 strtotime()

$d1 = strtotime('2016-02-02 07:35:00');
$d2 = strtotime('2016-02-1 13:10:31');
if($d1>$d2){
  return true;
} 
您可以使用

日期时间

if (new DateTime($d1) > new DateTime($d2) {
   return true;
}

尝试像这样strtotime()函数:

if(strtotime($d1) > strtotime($d2))
{
return true;
}

试试这个

$d1 = strtotime('2016-02-02 07:35:00');
$d2 = strtotime('2016-02-1 13:10:31');
if($d1 > $d2)
{
  return true;
} 

你可以这样比较:

<?
$d1 = '2016-02-02 07:35:00';
$d2 = '2016-02-1 13:10:31';
$ts1 = strtotime($d1); // 1454394900
$ts2 = strtotime($d2); // 1454328631
if($ts1>$ts2) // 1454394900 > 1454328631 = true
{
    echo 1; // return this.
}
else{
    echo 0;
}
?>

对日期和比比较都使用 strtotime()

<?php
$date = '1994-04-27 11:35:00';
$date1 = '2016-02-10 13:10:31';
$stt = strtotime($date); // 767439300
$stt1 = strtotime($date1); // 1455106231

if($stt>$stt1) 
    echo 1; 
else
    echo 0;

?>

使用StrtoTime函数将字符串转换为时间格式...