php - as3 - 格式化来自 mysql 的时间戳字符串

标签 php mysql actionscript-3 string formatting

我从一个 php 文件中接收到一个日期字段,我需要对其进行格式化。 我得到这个字符串:“2009-11-12 17:58:13” 我想转换为“2009 年 11 月 12 日下午 5:58:13”

我尝试使用 Date 类,但我得到的是这样的东西: 类型强制失败:无法将“2009-11-12 17:58:13”转换为日期。

有人知道这样做有什么好的实用程序吗?

最佳答案

如果没有直接的解决方案,您可以求助于正则表达式:

private function formatDate(str:String):String
{

    var regex:RegExp = /(\d+)-(\d+)-(\d+)\s(\d+):(\d+):(\d+)/;
    var matches:Object = regex.exec(str);       
    var date:Date = new Date(matches[1], Number(matches[2]) - 1, matches[3], 
        matches[4], matches[5], matches[6]);
    var months:Array = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", 
        "Sep", "Oct", "Nov", "Dec"];
    var result:String = months[date.month] + " ";
    result += date.date + ", " + date.fullYear + " ";
    if(date.hours > 12)
        result += (date.hours - 12) + ":" + date.minutes + ":" + 
            date.seconds + " pm";
    else
        result += date.hours + ":" + date.minutes + ":" + date.seconds + " am";
    return result;
}

正则表达式解释 /(\d+)-(\d+)-(\d+)\s(\d+):(\d+):(\d+)/

// Forward slashes - / - at the beginning and end are delimiters

\d+ /* One or more number of digits. 
     * Ideally it should be \d{n} (\d{4} - 4 digits), 
     * but I'm assuming that the input string would always be 
     * valid and properly formatted
     */

- and : //A literal hyphen/colon 

\s   //A whitespace (use \s+ to skip extra whitespaces)

()  /* Parenthetic groups used to capture a string for using it later
     * The return value of regex.exec is an array whose first element is 
     * the complete matched string and subsequent elements are contents
     * of the parenthetic groups in the order they appear in the regex
     */

关于php - as3 - 格式化来自 mysql 的时间戳字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1726586/

相关文章:

actionscript-3 - Google Chrome 中的 Flash 音频滞后

php - 具有多对多关系的 Symfony 3 uniqueEntity 验证

php - Mysql搜索字符串

mysql - snprintf 截断最后一个符号

apache-flex - AsUnit 与 FlexUnit – 哪个是 "better"?

android - as3 air 获取设备上文件的特定路径

php - Twig:如果 is_granted ('ROLE_MANAGER' ) 未授予检查

javascript - 单击一次即可执行多个 php 页面

php - 在 mysql 中检查所选日期和当前日期的差异

PHP 2 个日期之间