strtotime然后转换为UNIX?

发布于 2024-12-25 19:09:33 字数 673 浏览 1 评论 0原文

编辑

根据下面的评论,这是我在实现 mktime 之前尝试拆分/组合/转换字符串...

$date1 = explode("T",'2005-03-27T00:00:00');
$date2 = explode("-", $date1[0]);
$try = mktime($time1,$time2,0,$date2[1],$date[2],$date[3]);
print $try; 

// 打印输出:951793200

原始问题:

我已经继承了一个数据库,并且非常希望将数据存储的奇怪方式转换为对 mysql 更友好的方式...

与此同时,我得到一个 text 字符串(是的...文本)。 ..我愿意转换为 unixtime...

所以,我会得到一个如下所示的字符串:

2005-03-27T00:00:00 03:00 上午

我想将其转换为:

1111885200

日期和时间总是让我感到困惑...我已经使用 strtotime 和 mktime 做了很多事情,但无法按照我想要的方式对其进行格式化。

EDIT

Per the comment below, here's my attempt at splitting / combining / converting the string prior to implementing mktime...

$date1 = explode("T",'2005-03-27T00:00:00');
$date2 = explode("-", $date1[0]);
$try = mktime($time1,$time2,0,$date2[1],$date[2],$date[3]);
print $try; 

// Prints out: 951793200

Original Question:

I've inherited a database and would like very much to convert the bizarre way the data is stored to something more mysql-friendly...

In the meantime, I get a text string (yes... text)... That I'd like to convert to unixtime...

So, I'll get a string that looks like this:

2005-03-27T00:00:00 03:00 AM

I'd like to convert it to:

1111885200

Dates and times always mess me up... I've done a number of things using strtotime and mktime, but can't get it formatted the way I want.

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

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

发布评论

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

评论(4

允世 2025-01-01 19:09:33

嗯,我就是这样做的。

$ex = '2005-03-27T00:00:00 03:00 AM';
$format = '%Y-%m-%dT00:00:00 %H:%M %p';
$strf = strptime($ex, $format);
$date_str = $strf['tm_mon'] + 1 . '/' . $strf['tm_mday'] . '/' . ($strf['tm_year'] + 1900) . ' ' . $strf['tm_hour'] . ':' . $strf['tm_min'] . ':00';
echo $date_str;
echo "\n";
echo strtotime($date_str);
echo "\n";
echo date('m-d-Y H:i:s', 1111885200);

但是,根据您发布的日期,您想要的结果似乎不正确。

输出

3/27/2005 3:0:00
1111921200
03-26-2005 17:00:00

Well, this is how I would do it.

$ex = '2005-03-27T00:00:00 03:00 AM';
$format = '%Y-%m-%dT00:00:00 %H:%M %p';
$strf = strptime($ex, $format);
$date_str = $strf['tm_mon'] + 1 . '/' . $strf['tm_mday'] . '/' . ($strf['tm_year'] + 1900) . ' ' . $strf['tm_hour'] . ':' . $strf['tm_min'] . ':00';
echo $date_str;
echo "\n";
echo strtotime($date_str);
echo "\n";
echo date('m-d-Y H:i:s', 1111885200);

But, your desired result does not seem to be correct based on the date you posted.

OUTPUT

3/27/2005 3:0:00
1111921200
03-26-2005 17:00:00
有深☉意 2025-01-01 19:09:33

您可以编写一个小型转换器脚本来为您完成该任务。

请检查 mktime 的可选参数

您可以尝试将字符串拆分为小时/分钟/秒/月/日/年并将其放入 mktime 中。然后 mktime 会给你相应的 unix 时间戳。

You could write a litte converter script that does the task for you.

Please check the optional parameters of mktime

You could try to split your string in hour / minute / second / month / day / year and put that into mktime. Then mktime will give you the unix timestamp for that.

所有深爱都是秘密 2025-01-01 19:09:33

因此,除非绝对必要,否则我不喜欢正则表达式,但在这里可能需要它们来挑选时区部分。

preg_match("/(\d{4})-(\d{2})-(\d{2})(T\d{2}:\d{2}:\d{2}) (\d{2}:\d{2}) (AM|PM)/", '2005-03-27T00:00:00 03:00 AM',$matches);

将为您提供看起来像这样的 $matches:

Array
(
    [0] => 2005-03-27T00:00:00 03:00 AM
    [1] => 2005
    [2] => 03
    [3] => 27
    [4] => T00:00:00
    [5] => 03:00
    [6] => AM
)

我会将这些片段放入 DateTime 对象。然后您可以将其输出为您想要的任何 格式。您可能还需要调整该正则表达式,以便它可以处理您的所有日期。

So, I'm not a fan of regular expressions unless absolutely necessary, but they might be necessary here to pick out the time zone piece.

preg_match("/(\d{4})-(\d{2})-(\d{2})(T\d{2}:\d{2}:\d{2}) (\d{2}:\d{2}) (AM|PM)/", '2005-03-27T00:00:00 03:00 AM',$matches);

Will give you $matches that look something like:

Array
(
    [0] => 2005-03-27T00:00:00 03:00 AM
    [1] => 2005
    [2] => 03
    [3] => 27
    [4] => T00:00:00
    [5] => 03:00
    [6] => AM
)

I would feed those pieces into a DateTime object. Then you can output it into any format you want. You also may have to adjust that regex some so it can handle all your dates.

酒浓于脸红 2025-01-01 19:09:33

如果 T00:00:00 部分没用,只需将其删除并使用 strtotime()

<?php
$date = '2005-03-27T00:00:00 03:00 AM';
$timestamp = strtotime(preg_replace('!T[^ ]+!', '', $date));

var_dump(date('d/m/Y H:i:s', $timestamp));
?>

这会打印:

string(19) "27/03/2005 03:00:00"

If the T00:00:00 part is useless, just remove it and use strtotime():

<?php
$date = '2005-03-27T00:00:00 03:00 AM';
$timestamp = strtotime(preg_replace('!T[^ ]+!', '', $date));

var_dump(date('d/m/Y H:i:s', $timestamp));
?>

This prints:

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