简体   繁体   English

将天,月,年和国家/地区转换为YYYY-MM-DD

[英]Convert day, full month, year and country to YYYY-MM-DD

I'm trying to convert the string 18 December 2009 (Sweden) to 2009-12-18 but I can't figure out how. 我正在尝试将字符串18 December 2009 (Sweden)2009-12-18但我不知道如何。 So I asking you now: how can I do this? 所以我现在问你:我该怎么做?

Thanks in advance. 提前致谢。

You can use strtotime(): 您可以使用strtotime():

<?php
$sdate = '18 December 2009';
$timestamp = strtotime($sdate);
$d = date('Y-m-d', $timestamp);
echo "$d\n"; // 2009-12-18

If your problem is that the '(Sweden)' part is always present, you could just remove that part first: 如果您的问题是“(瑞典)”部分始终存在,则可以先删除该部分:

<?php
$sdate = '18 December 2009 (Sweden)';
$sdate = preg_replace('/ \(.*\)$/', '', $sdate);
$timestamp = strtotime($sdate);
$d = date('Y-m-d', $timestamp);
echo "$d\n"; // 2009-12-18

Or with added checking: 或添加检查:

<?php
$sdate = $oImdb->getReleaseDate();
if ($sdate !== 'n/A') {
    $sdate = preg_replace('/ \(.*\)$/', '', $sdate);
    $timestamp = strtotime($sdate);
    $d = date('Y-m-d', $timestamp);
} else {
    $d = 'n/a';
}
echo "$d\n"; // 2009-12-18

Or use sscanf(): 或使用sscanf():

<?php
$sdate = '18 December 2009 (Sweden)';
list($day, $month, $year) = sscanf($sdate, '%d %s %d');
$timestamp = strtotime("$day $month $year");
$d = date('Y-m-d', $timestamp);
echo "$d\n"; // 2009-12-18

First use Explode etc to remove the country name an store the Date and Country Name in separate strings, and then use strtotime 首先使用Explode等在单独的字符串中删除国家/地区名称,商店日期和国家/地区名称,然后使用strtotime

strtotime("18 December 2009")
strtotime(<String Variable containing date>)

It returns a timestamp, you can then use it as you want. 它返回一个时间戳,然后您可以根据需要使用它。 See this for reference 请参阅此作为参考

Then use Date to convert the timestamp to date in the format you want. 然后使用日期将时间戳转换为所需格式的日期。

There is also the PHP 5 DateTime object, which will allow you to capture time zones and convert time zones if start using times in addition to the date. 还有一个PHP 5 DateTime对象,如果开始使用除日期之外的其他时间,它将允许您捕获时区并转换时区。

<?php
$sdate = '18 December 2009 (Sweden)';
$sdate = preg_replace('/ \(.*\)$/', '', $sdate);
$datetime = new DateTime($sdate, new DateTimeZone('UTC'));
echo $datetime->format('Y-m-d');  // 2009-12-18

The DateTime object will allow you to manipulate the date without any seconds arithmetic, which can be a reason to use DateTime instead of strtotime(). DateTime对象将允许您无需任何秒数的运算就可以操作日期,这可能是使用DateTime而不是strtotime()的原因。

For example, this would add one month to your original date. 例如,这将使您的原始日期增加一个月。 Many benefits of the DateTime class. DateTime类的许多好处。

$datetime->add(new DateInterval('P1M'));

I am not sure what you are doing there, but judging from the error message you wrote in your comment to JM above, I'd say you are approaching the problem from the wrong end. 我不确定您在做什么,但是从您在上面对JM的评论中写的错误消息来看,我认为您是从错误的角度来解决问题。 Quoting: 报价:

Thanks! 谢谢! When I try the function with $oIMDB->getReleaseDate() I get following error messages: 当我尝试使用$oIMDB->getReleaseDate()函数时,收到以下错误消息:

Fatal error: Uncaught exception 'Exception' with message DateTime::__construct() [datetime.--construct]: Failed to parse time string ( <time itemprop="datePublished" datetime="2009-12-18">18 December 2009</time> ) … 致命错误:消息为DateTime::__construct() [datetime .-- construct]的未捕获异常'Exception':无法解析时间字符串( <time itemprop="datePublished" datetime="2009-12-18">18 December 2009</time> )…

In other words, your getReleaseDate returns an XML string that has an attribute datetime with the value you are trying to convert the element value to. 换句话说,您的getReleaseDate返回一个XML字符串,该字符串具有datetime属性,该属性具有您尝试将元素值转换为的值。 So, there is no need to convert anything, because the converted value is already there. 因此,无需进行任何转换,因为转换后的值已经存在。 All you have to do is use SimpleXml or DOM and access that attribute value, eg 您所要做的就是使用SimpleXmlDOM并访问该属性值,例如

$time = simplexml_load_string($oIMDB->getReleaseDate());
echo $time['datetime'];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM