Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/265.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 如何将时间格式从数字转换为字符串?_Php_Cakephp_Cakephp 1.3 - Fatal编程技术网

Php 如何将时间格式从数字转换为字符串?

Php 如何将时间格式从数字转换为字符串?,php,cakephp,cakephp-1.3,Php,Cakephp,Cakephp 1.3,我从创建的字段日期和时间中提取,格式如下2011-3-10 17:26:50,我想转换成这种格式2010年3月13日如何使用php或cakephp 1.3实现这一点 <?php $i = 0; foreach($articles as $$article ): ?> <tr> <td><?php echo $article['Art

我从创建的字段日期和时间中提取,格式如下
2011-3-10 17:26:50
,我想转换成这种格式
2010年3月13日
如何使用php或cakephp 1.3实现这一点

     <?php

  $i = 0;
foreach($articles as $$article ):

                 ?>
                <tr>
                    <td><?php echo $article['Article']['title']; ?></td>
                    <td><?php echo $article['Article']['created'];;?></td>
                </tr>
                <?php endforeach; ?>

像这样的东西? 我猜你的日期时间格式。您可能需要调整它

echo date('F d, Y',date_create_from_format('Y-n-d h:i:s','2011-3-10 17:26:50'));
像这样的事? 我猜你的日期时间格式。您可能需要调整它

echo date('F d, Y',date_create_from_format('Y-n-d h:i:s','2011-3-10 17:26:50'));

我猜您的意思是从MySQL检索时的“获取”。最简单/最快的(但也是最喜欢炸掉和踢你的狗)就是简单地做

$timestamp = strtotime($date_from_database);
$formatted = date('F j, Y', $timestamp);
我想你是指从MySQL中检索时的“获取”。最简单/最快的(但也是最喜欢炸掉和踢你的狗)就是简单地做

$timestamp = strtotime($date_from_database);
$formatted = date('F j, Y', $timestamp);

PHP有一个函数
strottime($string)
,它将以各种格式(包括MySQL的datetime格式)获取日期/时间字符串,并将其转换为Unix时间戳整数(自1970-01-01 00:00:00 UTC以来的秒数)。然后可以使用
date('fj,Y',$time)
使用找到的标记将该整数转换为所需的任何字符串表示形式

另外两个考虑因素是本地化和时区感知。我将不讨论第一个,因为您似乎不需要它,但是在时区重要的地方,使用PHP的DateTime类会更容易,您可以阅读这些类。下面是一个例子:

<?php

// for example, if you're storing times in UTC in your DB
$dbTimezone = new DateTimeZone('UTC');

// we'll use this for displaying times in the user's timezone.
// for example, my timezone:
$displayTimezone = new DateTimeZone('America/Toronto');

foreach ($articles as $article):
    // Create a timezone-aware DateTime object from your article's creation date
    $dt = new DateTime($article['Article']['create'], $dbTimezone);
    $dt->setTimezone($displayTimezone);
    echo $dt->format('F j, Y');
endforeach;

PHP有一个函数
strottime($string)
,它将以各种格式(包括MySQL的datetime格式)获取日期/时间字符串,并将其转换为Unix时间戳整数(自1970-01-01 00:00:00 UTC以来的秒数)。然后可以使用
date('fj,Y',$time)
使用找到的标记将该整数转换为所需的任何字符串表示形式

另外两个考虑因素是本地化和时区感知。我将不讨论第一个,因为您似乎不需要它,但是在时区重要的地方,使用PHP的DateTime类会更容易,您可以阅读这些类。下面是一个例子:

<?php

// for example, if you're storing times in UTC in your DB
$dbTimezone = new DateTimeZone('UTC');

// we'll use this for displaying times in the user's timezone.
// for example, my timezone:
$displayTimezone = new DateTimeZone('America/Toronto');

foreach ($articles as $article):
    // Create a timezone-aware DateTime object from your article's creation date
    $dt = new DateTime($article['Article']['create'], $dbTimezone);
    $dt->setTimezone($displayTimezone);
    echo $dt->format('F j, Y');
endforeach;
这将解决这个问题:

$date = date_create_from_format('Y-m-j H:i:s', $article['Article']['created']);
echo date_format($date, 'F d, Y');
这将解决这个问题:

$date = date_create_from_format('Y-m-j H:i:s', $article['Article']['created']);
echo date_format($date, 'F d, Y');

您可以轻松地使用Cakephp时间助手

//Add to appcontroller or articles_controller
var $helpers = array('Time');

//Add this to view file
echo $this->Time->niceShort($article['Article']['created']); 
除了
niceShort
之外,还有更多的选项可以更好地满足您的需求。查看CakePHP文档


谢谢,

您可以轻松使用Cakephp时间助手

//Add to appcontroller or articles_controller
var $helpers = array('Time');

//Add this to view file
echo $this->Time->niceShort($article['Article']['created']); 
除了
niceShort
之外,还有更多的选项可以更好地满足您的需求。查看CakePHP文档


谢谢,

如果你不想踢你的狗,请使用。如果你不想踢你的狗,请使用。