如何在 Perl & 中根据日期时间比较两个字符串php?

发布于 2024-11-19 13:04:32 字数 104 浏览 0 评论 0 原文

我有(日期时间)格式的字符串 - “Mon, 11 Jul 2011 11:45:07 +08:00”,我需要根据日期时间格式比较此类字符串,以查找最新的字符串。我应该在这里使用哪个模块/方法?

I have strings in the (date-time) format- "Mon, 11 Jul 2011 11:45:07 +08:00", I need to compare such strings based on the date-time format, for finding most recent string. Which module/method should i used here ?

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

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

发布评论

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

评论(5

梦晓ヶ微光ヅ倾城 2024-11-26 13:04:32
use DateTime::Format::Strptime qw();

my $time_str = 'Mon, 11 Jul 2011 11:45:07 +08:00';
# your notation of time zone
# does not comply with relevant standards
$time_str    =~ s/([+-]\d\d):(\d\d) \z/$1$2/msx; # kill colon

my $parser   = DateTime::Format::Strptime->new(
    pattern  => '%a, %d %b %Y %T %z',
    locale   => 'en',   # 'Mon', 'Jul' are English
    on_error => 'croak',
);

my $datetime = $parser->parse_datetime($time_str);

# Now you have a DateTime object. To compare,
# use the overloaded relation operators.
# <=> operator/sort function works as well.

if ($datetime < $a_different_datetime) {
    say 'earlier';
}
use DateTime::Format::Strptime qw();

my $time_str = 'Mon, 11 Jul 2011 11:45:07 +08:00';
# your notation of time zone
# does not comply with relevant standards
$time_str    =~ s/([+-]\d\d):(\d\d) \z/$1$2/msx; # kill colon

my $parser   = DateTime::Format::Strptime->new(
    pattern  => '%a, %d %b %Y %T %z',
    locale   => 'en',   # 'Mon', 'Jul' are English
    on_error => 'croak',
);

my $datetime = $parser->parse_datetime($time_str);

# Now you have a DateTime object. To compare,
# use the overloaded relation operators.
# <=> operator/sort function works as well.

if ($datetime < $a_different_datetime) {
    say 'earlier';
}
格子衫的從容 2024-11-26 13:04:32

我在 Perl 中使用 Date::Manip为了这:

use Date::Manip;

my $datestr1 = "Mon, 11 Jul 2011 11:45:07 +08:00";
my $datestr2 = "Mon, 18 Jul 2011 11:45:07 +08:00";

my $date1 = new Date::Manip::Date;
my $date2 = $date1->new_date;
$date1->parse($datestr1);
$date2->parse($datestr2);

my $result = $date1->cmp($date2); # => -1, 0, 1

print $result, "\n";

I use Date::Manip in Perl for this:

use Date::Manip;

my $datestr1 = "Mon, 11 Jul 2011 11:45:07 +08:00";
my $datestr2 = "Mon, 18 Jul 2011 11:45:07 +08:00";

my $date1 = new Date::Manip::Date;
my $date2 = $date1->new_date;
$date1->parse($datestr1);
$date2->parse($datestr2);

my $result = $date1->cmp($date2); # => -1, 0, 1

print $result, "\n";
爱*していゐ 2024-11-26 13:04:32

对于 PHP,最简单的方法是使用 strtotime() 转换时间字符串并比较整数值。最大的整数是最新的。

请注意,如果您未设置默认时区,某些版本的 PHP 会在使用日期函数时向您发出警告。您可以使用 date_default_timezone_set("") 或 php.ini 的 date.timezone = "" 以及 支持的时区列表

For PHP, the easiest way would be converting the time strings with strtotime() and comparing the integer values. Highest integer is the latest.

Note that some versions of PHP will warn you when using Date functions if you have not set your default timezone. You can do so with date_default_timezone_set("") or php.ini's date.timezone = "", with the list of supported timezones.

神魇的王 2024-11-26 13:04:32

在 PHP 中,称为 strtotime()

In PHP it's called strtotime()

一身仙ぐ女味 2024-11-26 13:04:32

您可以在您拥有的时间戳上尝试 strtotime() 。然后只需使用普通比较器比较数字即可。

you can try strtotime() on the timestamp you have. then just compare the numbers using normal comparators.

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