MySQL DATETIME 格式比较 - 是否需要 strtotime?

发布于 2024-08-25 05:03:08 字数 369 浏览 4 评论 0原文

我一直在做一些事情......

$dt1 = '1000-01-01 00:00:00'; //really some val from db
$dt2 = '1000-01-01 00:00:10'; //another val maybe db maybe formatted

if(strtotime($dt1) > strtotime($dt2){
//do something
}

是否需要 strtotime?我可以对日期时间格式的字符串进行更直接的比较吗?

if($dt1 > $dt2){
//do something
}

这总是有效吗?

I've been doing something along the lines of..

$dt1 = '1000-01-01 00:00:00'; //really some val from db
$dt2 = '1000-01-01 00:00:10'; //another val maybe db maybe formatted

if(strtotime($dt1) > strtotime($dt2){
//do something
}

Is the strtotime needed? can i do a more direct comparison on the datetime formatted strings?

i.e.

if($dt1 > $dt2){
//do something
}

Will that always work?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

走过海棠暮 2024-09-01 05:03:08

是的,字符串的字典顺序格式为yyyy-mm-dd hh: ii:ss 按预期工作。您甚至可以像这样对日期进行排序。

$dts = array(
  '1000-01-01 00:00:00',
  '2010-03-16 21:22:19',
  '1000-01-01 00:00:10',
  '1976-03-27 05:55:00', 
  '1976-03-27 05:54:00',
  '1968-08-21 12:00:00',
  '2001-01-01 00:00:01'
);

sort($dts);
foreach($dts as $dt) {
  echo $dt, "\n";
}

1000-01-01 00:00:00
1000-01-01 00:00:10
1968-08-21 12:00:00
1976-03-27 05:54:00
1976-03-27 05:55:00
2001-01-01 00:00:01
2010-03-16 21:22:19

请记住,对于格式不正确的字符串,您将不会收到任何反馈。因此,如果可能存在格式错误的字符串,您最好也检查一下。如果这不是问题,则 string1>string2 就可以了。

编辑:你甚至可以让 MySQL 来做这项工作。这是否比在 php 中更好/等于/更差取决于您实际想要实现的目标。例如

$pdo = new PDO("mysql:host=localhost;dbname=test", 'localonly', 'localonly'); 
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
// setting up a sample table with some values
$pdo->exec('CREATE TEMPORARY TABLE foo (id int auto_increment, d datetime NOT NULL, primary key(id))');
$pdo->exec("INSERT INTO foo (d) VALUES ('1000-01-01 00:00:00'),('2010-03-16 21:22:19'),('1000-01-01 00:00:10'),('1976-03-27 05:55:00')");


$query = "
  SELECT
    d,
    (d>'1910-03-17 12:00:00') as flag
  FROM
    foo
";

foreach ( $pdo->query($query) as $row ) {
  echo $row['flag'] ? '+ ':'- ', $row['d'], "\n";
}

印刷品

- 1000-01-01 00:00:00
+ 2010-03-16 21:22:19
- 1000-01-01 00:00:10
+ 1976-03-27 05:55:00

Yes, the lexicographical order of strings in the format yyyy-mm-dd hh:ii:ss works as intended. You could even sort dates like that.

$dts = array(
  '1000-01-01 00:00:00',
  '2010-03-16 21:22:19',
  '1000-01-01 00:00:10',
  '1976-03-27 05:55:00', 
  '1976-03-27 05:54:00',
  '1968-08-21 12:00:00',
  '2001-01-01 00:00:01'
);

sort($dts);
foreach($dts as $dt) {
  echo $dt, "\n";
}

prints

1000-01-01 00:00:00
1000-01-01 00:00:10
1968-08-21 12:00:00
1976-03-27 05:54:00
1976-03-27 05:55:00
2001-01-01 00:00:01
2010-03-16 21:22:19

But keep in mind that you will get no feedback for strings that are not in the right format. So, if it could be that there are malformed strings you'd better check that, too. If that is not a concern, string1>string2 is ok.

edit: You could even let MySQL do the work. If this is better/equal/worse to doing it in php depends on what you're actually trying to achieve. E.g.

$pdo = new PDO("mysql:host=localhost;dbname=test", 'localonly', 'localonly'); 
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
// setting up a sample table with some values
$pdo->exec('CREATE TEMPORARY TABLE foo (id int auto_increment, d datetime NOT NULL, primary key(id))');
$pdo->exec("INSERT INTO foo (d) VALUES ('1000-01-01 00:00:00'),('2010-03-16 21:22:19'),('1000-01-01 00:00:10'),('1976-03-27 05:55:00')");


$query = "
  SELECT
    d,
    (d>'1910-03-17 12:00:00') as flag
  FROM
    foo
";

foreach ( $pdo->query($query) as $row ) {
  echo $row['flag'] ? '+ ':'- ', $row['d'], "\n";
}

prints

- 1000-01-01 00:00:00
+ 2010-03-16 21:22:19
- 1000-01-01 00:00:10
+ 1976-03-27 05:55:00
删除会话 2024-09-01 05:03:08

如果您使用 YYYY-MM-DD HH:MM:SS 格式以字符串形式存储日期,则可以使用字母顺序:它将与日期相同命令。

这意味着字符串比较与日期比较具有相同的结果。

我想补充一点,不使用 UNIX 时间戳 会有一个优势:这些时间戳仅限于范围从 1970 年到 2038 年,因为它们存储在 32 位整数中,表示自 1970 年 1 月 1 日以来的秒数(根据您的系统,范围可能更宽 - 但不是无限的)

If you have dates stored in string, using YYYY-MM-DD HH:MM:SS format, you can use the alphabetical order : it'll be the same as dates order.

Which means the comparisons on strings will have the same result as comparisons on dates.

I would add that there would be an advantage in not using UNIX timestamps : those are limited to a range that goes from 1970 to 2038, as they are stored on 32 bits integers that represent a number of seconds since 1970-01-01 (depending on your system, the range might be wider -- but not unlimited)

迷雾森÷林ヴ 2024-09-01 05:03:08

我通常使用 strtotime() 来安全地将日期作为时间戳值进行比较。

您可以通过编写 IF 语句并在其中一个中打印“statement 1 true”并在另一个中打印“statement 2 true”来进行检查。然后尝试更改 dt1 和 dt2 的值,看看结果如何。

I usually use strtotime() to be safe in comparing dates as a timestamp value.

You can check by writing both IF statements and printing 'statement 1 true' in one and 'statement 2 true' in the other. Then try changing the values of dt1 and dt2 and see how that plays out.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文