简体   繁体   中英

PHP: Why converting a date string to a timestamp then back to a date string doesn't work?

My code

$time = "Tuesday, 26 June 2012";
//str_replace(',','',$time);<--this also doesn't work.
$a = strptime($time, "%l, %j %F %Y");
$stmp = mktime(0,0,0,$a['tm_mon'],$a['tm_mday'],$a['tm_year'],0);
$from_stmp = date("l, j F Y H:i:s", $stmp);
echo $from_stmp; //Tuesday, 30 November 1999 00:00:00

Now i know there is a more elegant way, that actually works:

$time = "Tuesday, 26 June 2012";
$stmp = strtotime($time);
$from_stmp = date("l, j F Y H:i:s", $stmp);
echo $from_stmp;//Tuesday, 26 June 2012 00:00:00

But what's wrong with the first version? I'm just curious.

Problem #1

$a = strptime(time, "%l, %j %F %Y");

You wrote time ; it should be $time .

Problem #2

Your format string is wrong. strptime doesn't use the same format strings as date , just with percentage signs in front; it has its own set. Your format string should look like this:

$a = strptime($time, "%A, %e %B %Y");

Problem #3

  • strptime returns a number of years since 1900 . You need to add 1900 .
  • strptime returns a month from 0 to 11. You need to add 1 .

All summed up

Here's your code, fixed:

<?php
$time = "Tuesday, 26 June 2012";
$a = strptime($time, "%A, %e %B %Y");

$stmp = mktime(0, 0, 0, $a['tm_mon'] + 1, $a['tm_mday'], 1900 + $a['tm_year'], 0);
$from_stmp = date("l, j F Y H:i:s", $stmp);
echo $from_stmp;
?>

Hooray, it works!

You still have to remove the last 0 of the mktime function it should be correctly,

$time = "Tuesday, 26 June 2012";
$a = strptime($time, "%A, %e %B %Y");

$stmp = mktime(0, 0, 0, $a['tm_mon'] + 1, $a['tm_mday'], 1900 + $a['tm_year']);
$from_stmp = date("l, j F Y H:i:s", $stmp);
echo $from_stmp;

The format of $time is wrong, it should be "%A, %e %B %Y".

If you var_dump $a ,

array(9) {
  ["tm_sec"]=>
  int(0)
  ["tm_min"]=>
  int(0)
  ["tm_hour"]=>
  int(0)
  ["tm_mday"]=>
  int(26)
  ["tm_mon"]=>
  int(5)
  ["tm_year"]=>
  int(112)
  ["tm_wday"]=>
  int(2)
  ["tm_yday"]=>
  int(177)
  ["unparsed"]=>
  string(0) ""
}

If you see the docs, you will find the actual values returned.

"tm_sec"    Seconds after the minute (0-61)
"tm_min"    Minutes after the hour (0-59)
"tm_hour"   Hour since midnight (0-23)
"tm_mday"   Day of the month (1-31)
"tm_mon"    Months since January (0-11)// increment month
"tm_year"   Years since 1900// add years from 1900
"tm_wday"   Days since Sunday (0-6)
"tm_yday"   Days since January 1 (0-365)
"unparsed"  the date part which was not recognized using the specified format

you find the actual values passed into your mktime, Adjust your vals passed into mktime to fix this.

<?
$time = "Tuesday, 26 June 2012";
$a = strptime($time, "%A, %e %B %Y");
var_dump($a);
$stmp = mktime(0, 0, 0, $a['tm_mon'] + 1, $a['tm_mday'], 1900 + $a['tm_year']);
$from_stmp = date("l, j F Y H:i:s", $stmp);
echo $from_stmp;
?>

Well because strptime() is outdated !

From the php docs : (PHP 5 >= 5.1.0)

So you should use strtotime() ...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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