将字符串转换为 UNIX 时间戳

发布于 2024-12-17 15:40:33 字数 228 浏览 0 评论 0原文

我正在尝试将字符串 11/24/2011 @ 01:15pm 转换为 UNIX 时间戳。格式为 mdY @ h:ia

我似乎无法让 strtotime 处理该字符串。有没有办法反转数据功能?我唯一的选择是创建一个新的非默认 php 函数来转换字符串吗?

服务器运行 CentOS 5、Apache 2.2 和 PHP 5.2.17。

I'm trying to convert the string 11/24/2011 @ 01:15pm to a UNIX timestamp. The format is m-d-Y @ h:ia

I can't seem to get strtotime to work with the string. Is there a way to reverse the data function? Is my only choice to create a new non-default php function to convert the string?

The server is running CentOS 5, Apache 2.2 and PHP 5.2.17.

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

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

发布评论

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

评论(5

琉璃梦幻 2024-12-24 15:40:33

使用更现代的 DateTime 类(所以只要您使用 >= 5.3)。

$unix = DateTime::createFromFormat('m/d/Y @ h:ia', '11/24/2011 @ 01:15pm')
        ->getTimestamp();

键盘

Use the more modern DateTime class (so long as you're using >= 5.3).

$unix = DateTime::createFromFormat('m/d/Y @ h:ia', '11/24/2011 @ 01:15pm')
        ->getTimestamp();

CodePad.

时间你老了 2024-12-24 15:40:33

在 PHP 5.2 下,您可以使用 strptime 来解析具有特定日期时间字符串格式,然后使用 mktime 将结果转换为时间戳。

$timeString = '11/24/2011 @ 01:15pm';

$timeArray = strptime($timeString, '%m/%d/%Y @ %I:%M%p');
$timestamp = mktime(
    $timeArray['tm_hour'], $timeArray['tm_min'], $timeArray['tm_sec'], 
    $timeArray['tm_mon']+1, $timeArray['tm_mday'], $timeArray['tm_year']+1900
);

这应该被抽象为一个函数,可能有两个:

function strptimestamp($date, $fmt) {
    $timeArray = strptime($date, $fmt);
    return mktime(
        $timeArray['tm_hour'], $timeArray['tm_min'], $timeArray['tm_sec'], 
        $timeArray['tm_mon']+1, $timeArray['tm_mday'], $timeArray['tm_year']+1900
    );
}

function strpmy($date) {
    return strptimestamp($date, '%m/%d/%Y @ %I:%M%p');
}

对解析句点缩写的支持似乎因操作系统而异。如果上述方法在特定操作系统上不起作用,请尝试使用“%P”而不是“%p”或通过 strtoupper 传递时间字符串(或两者)。以下内容应该适用于任何操作系统,但最好让 strptime 来处理整个解析,因为以下内容不太适合作为通用 strptimestamp 函数的基础。

static $pm_abbrevs = array('pm' => 1, 'p.m.' => 1, 'µµ' => 1, 'µ.µ.' => 1);
$timeString = '11/24/2011 @ 01:15pm';

$timeArray = strptime($timeString, '%m/%d/%Y @ %I:%M');
$period = strtolower($timeArray['unparsed']);
if (isset($pm_abbrevs[$period])) {
    $timeArray['tm_hour'] += 12; 
}
$timestamp = mktime(
    $timeArray['tm_hour'], $timeArray['tm_min'], $timeArray['tm_sec'], 
    $timeArray['tm_mon']+1, $timeArray['tm_mday'], $timeArray['tm_year']+1900
);

Under PHP 5.2, you can use strptime to parse a date-time string with a specific format, then use mktime to convert the result to a timestamp.

$timeString = '11/24/2011 @ 01:15pm';

$timeArray = strptime($timeString, '%m/%d/%Y @ %I:%M%p');
$timestamp = mktime(
    $timeArray['tm_hour'], $timeArray['tm_min'], $timeArray['tm_sec'], 
    $timeArray['tm_mon']+1, $timeArray['tm_mday'], $timeArray['tm_year']+1900
);

This should be abstracted as a function, possibly two:

function strptimestamp($date, $fmt) {
    $timeArray = strptime($date, $fmt);
    return mktime(
        $timeArray['tm_hour'], $timeArray['tm_min'], $timeArray['tm_sec'], 
        $timeArray['tm_mon']+1, $timeArray['tm_mday'], $timeArray['tm_year']+1900
    );
}

function strpmy($date) {
    return strptimestamp($date, '%m/%d/%Y @ %I:%M%p');
}

Support for parsing the period abbreviation appears to vary from OS to OS. If the above doesn't work on a particular OS, try "%P" instead of "%p" or pass the time string through strtoupper (or both). The following should work under any OS, though it's preferable to get strptime to handle the entirety of the parsing, as the following is less suitable as the basis for a generic strptimestamp function.

static $pm_abbrevs = array('pm' => 1, 'p.m.' => 1, 'µµ' => 1, 'µ.µ.' => 1);
$timeString = '11/24/2011 @ 01:15pm';

$timeArray = strptime($timeString, '%m/%d/%Y @ %I:%M');
$period = strtolower($timeArray['unparsed']);
if (isset($pm_abbrevs[$period])) {
    $timeArray['tm_hour'] += 12; 
}
$timestamp = mktime(
    $timeArray['tm_hour'], $timeArray['tm_min'], $timeArray['tm_sec'], 
    $timeArray['tm_mon']+1, $timeArray['tm_mday'], $timeArray['tm_year']+1900
);
亽野灬性zι浪 2024-12-24 15:40:33

如果将“@”替换为空格,那么 strtotime 应该能够本地理解它。

<?php
$x = "11/24/2011 @ 01:15pm";
$x = str_replace(" @ ", " ", $x);
$y = strtotime($x);
$z = date("m-d-Y @ h:ia", $y);
echo "x: $x<br />\n";
echo "y: $y<br />\n";
echo "z: $z<br />\n";
?>

输出:

x: 11/24/2011 01:15pm
y: 1322140500
z: 11-24-2011 @ 01:15pm

If you replace the ' @ ' with a space, then strtotime should be able to understand it natively.

<?php
$x = "11/24/2011 @ 01:15pm";
$x = str_replace(" @ ", " ", $x);
$y = strtotime($x);
$z = date("m-d-Y @ h:ia", $y);
echo "x: $x<br />\n";
echo "y: $y<br />\n";
echo "z: $z<br />\n";
?>

Output:

x: 11/24/2011 01:15pm
y: 1322140500
z: 11-24-2011 @ 01:15pm
少年亿悲伤 2024-12-24 15:40:33

区分大小写可能是您的问题 http://php.net/manual/en/datetime.formats .php
也许首先通过 strtoupper() 运行 $x,然后 str_replace('@', '', $x) (注意它用空字符串替换 @),然后尝试 <代码>strtotime()。希望这有帮助。

Case sensitivity may be your issue http://php.net/manual/en/datetime.formats.php.
Perhaps run $x through strtoupper() first then str_replace('@', '', $x) (notice it's replacing @ with an empty string), then try strtotime(). Hope this helps.

八巷 2024-12-24 15:40:33
$search = array('/',',','@');
$replace = array('-','','');
echo strtotime( str_replace($search,$replace,'16/7/2013 @ 7:30AM') );

这将替换您尝试转换为 strtotime 可接受的格式的时间字符串中的字符串部分。您始终可以将更多要替换的字符串部分添加到数组中。

此外,您不需要为此拥有最新的 php。

输出:

1373952600
$search = array('/',',','@');
$replace = array('-','','');
echo strtotime( str_replace($search,$replace,'16/7/2013 @ 7:30AM') );

this will replace the parts of the string in the time string you are trying to convert into a format that is acceptable to strtotime. You can always add more string parts you want to replace to the arrays.

Also you dont need to have latest php for this.

Output:

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