2

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.

4
  • 5
    is time a typo? Should it be $time? Commented Jun 24, 2012 at 20:03
  • Yes, typo. Fixed it, but the output doesn't change. Commented Jun 24, 2012 at 20:08
  • Your $a = strptime(time, "%l, %j %F %Y"); returns false, So strptime is not parsing properly. Problem should be with the input date. Commented Jun 24, 2012 at 20:15
  • It still is "time", not "$time" Commented Jun 24, 2012 at 20:30

4 Answers 4

4

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']);
$from_stmp = date("l, j F Y H:i:s", $stmp);
echo $from_stmp;
?>

Hooray, it works!

Sign up to request clarification or add additional context in comments.

2 Comments

Thx, i saw my error with the different format string... but, your solution didn't really worked. Tuesday, 26 June 2012 != Saturday, 26 May 2012
@alex: Sorry, missed one problem :) I updated my answer. It really works now!
2

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;

3 Comments

The 0 is an optional parameter and will work just fine, read the docs.
But when I tried to use with the 0 at the end I got this error, Deprecated: mktime() [function.mktime]: The is_dst parameter is deprecated
@itsme: You should probably just post that as a comment instead, then. I realize you don't have enough reputation, though - just for future reference. (Also, that's not an error, it's a warning.)
1

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;
?>

Comments

0

Well because strptime() is outdated !

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

So you should use strtotime() ...

4 Comments

Well i did test it with PHP 5.4.X and i get an error ` Call to undefined function strptime() ` so i quick searched in the php docs !
It's not implemented on Windows platforms.
@minitech thanks, so now i should definitely use strtotime for cross-compatibility !
The docs say, It can only parse an output of strftime(). php.net/manual/en/function.strptime.php strptime — Parse a time/date generated with strftime()

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.