开发者

php date is wrong?

开发者 https://www.devze.com 2023-02-18 08:28 出处:网络
Let\'s say I have this: $a11 = date(\"F j, Y, g:i a\", $a[\'date\']); $newTime = date($a[\'date\'], strtotime(\'+3 hour\'));

Let's say I have this:

$a11 = date("F j, Y, g:i a", $a['date']); 
$newTime = date($a['date'], strtotime('+3 hour'));
$b11 = date("F j, Y, g:i a", $newTime); 
echo $a11 . " AND " . $b11;

I know $a['date'] is right because I get: March 22, 2011, 10:22 pm. However, the echo produces: March 22, 2011, 10:22 pm AND March 22, 2011, 10:22 pm when clearly the second part is suppose to be three hours ahead.开发者_如何学编程

What am I doing wrong?


Don't you want:

$newTime = strtotime( '+3 hours',$a['date'] );
$b11 = date("F j, Y, g:i a", $newTime );


It seems you provide the wrong order of parameters in $newTime = date($a['date'], strtotime('+3 hour'));. Try this:

<?php
$a['date'] = mktime();
$a11 = date("F j, Y, g:i a", $a['date']); 
$newTime = date(strtotime('+3 hour'),$a['date']);
$b11 = date("F j, Y, g:i a", $newTime); 
echo $a11 . " AND " . $b11;
?>


Dig it, you are not strtotime'ing the $newTime when converting to date, so it's false.

<?php
$a['date'] = time();

$a11 = date("F j, Y, g:i a", $a['date']);

echo 'Now       = ' . time() . PHP_EOL;
echo 'Now +3hrs = ' . strtotime( '+3 hours' ) . PHP_EOL . PHP_EOL;

$newTime = strtotime( '+3 hours' );

$b11 = date("F j, Y, g:i a", $newTime );

echo $a11 . ' and ' . $b11 . PHP_EOL;


The format of date function is: string date ( string $format [, int $timestamp ] ). So, according to the first line, $a['date'] stores the timestamp value. But, according to the second line, its value is date format.

Moreover, you should type "+3 hours".


I add date like following

<?php
$a['date']="March 22, 2011, 10:22 pm";
$a11 = date("F j, Y, g:i a", strtotime($a['date'])); 
$b11 = strtotime(date("F j, Y, g:i a", strtotime($a['date'])) . " +3 hours");
$b11 = date("F j, Y, g:i a", $b11); 
echo $a11 . "AND " . $b11;
?>
0

精彩评论

暂无评论...
验证码 换一张
取 消