Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/date/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 将字符串转换为UNIX时间戳_Php_Date_Time_Strtotime_Unix Timestamp - Fatal编程技术网

Php 将字符串转换为UNIX时间戳

Php 将字符串转换为UNIX时间戳,php,date,time,strtotime,unix-timestamp,Php,Date,Time,Strtotime,Unix Timestamp,我正在尝试将字符串11/24/2011@01:15pm转换为UNIX时间戳。格式为m-d-Y@h:ia 我似乎无法使用字符串strotime。有没有办法反转数据功能?我唯一的选择是创建一个新的非默认php函数来转换字符串吗 服务器正在运行CentOS 5、Apache 2.2和PHP 5.2.17。使用更现代的类(只要使用>=5.3) .如果将“@”替换为空格,则strotime应该能够以本机方式理解它 <?php $x = "11/24/2011 @ 01:15pm"; $x = str

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

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

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

使用更现代的类(只要使用>=5.3)


.

如果将“@”替换为空格,则strotime应该能够以本机方式理解它

<?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";
?>

在PHP5.2下,可以使用解析具有特定格式的日期时间字符串,然后使用将结果转换为时间戳

$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
);

区分大小写可能是您的问题。 可能先通过
strotupper()
运行$x,然后再运行
str_replace('@','$x)
(注意它用空字符串替换@),然后尝试
strotime()
。希望这有帮助

$search = array('/',',','@');
$replace = array('-','','');
echo strtotime( str_replace($search,$replace,'16/7/2013 @ 7:30AM') );
这将替换您试图转换为strotime可接受格式的时间字符串中的字符串部分。始终可以向数组中添加更多要替换的字符串部分

此外,您不需要使用最新的php

输出:

x: 11/24/2011 01:15pm
y: 1322140500
z: 11-24-2011 @ 01:15pm
1373952600
1373952600如果您不想转换为“unix”,那就完全不同了。修正了你的术语。你所说的“非默认”函数是什么意思?其余的系统细节(OS&c)是什么?它是一个常规的cpanel服务器。Centos 5、apache 2.2、,etc@user962449:相关信息应编辑到问题中,而不是作为评论发布。访问者不应该为了理解问题或答案而阅读评论。你也应该考虑选择A。这样做的一个好处是,其他人可以使用,您将收到一个通知,说明有人在注释中向您发送了地址。我收到了以下错误:
致命错误:调用未定义的方法DateTime::createFromFormat()
@user962449:您可能有一个较旧的PHP版本(5.3之前)。您可以预处理字符串以适应
strotime()
。回答得好-您应该在回答中指出所需的PHP版本-而不仅仅是注释。也不起作用$y没有任何价值$x和z美元还行。很抱歉,$z已经
12-31-1969@04:00pm
,因为$y没有价值。我在发布之前在我的web服务器上运行了它。我将检查我的PHP版本。我运行的是相同的版本-5.2.17。我也喜欢这个答案,只是我无法让它工作。这很可能是因为“注意:在内部,此函数调用系统的C库提供的strtime()函数。此函数在不同的操作系统中可能表现出明显不同的行为。”“无法使其工作”很少说。确切地说,它是如何不起作用的?非常奇怪,但我必须将
%P
更改为
%P
,才能使这个示例起作用。请注意,这与文档所说的完全相反。它看起来不支持“%P”,但有一个“%P”。根据PHP的文档,OSX也不支持“%P”,尽管这不是我观察到的。此外,'%p'在OSX上解析小写的'pm'。 1373952600