0

I have this code with me and this is working fine on code.org but on my server this is just echoing 1970-1-1

<?php
$str = 'Posted: Thu Sep 05, 2013 3:40 pm ';
$listdate = preg_replace('/Posted: /','',$str );
$listdate = date('Y-m-d',strtotime($listdate)); 
echo $listdate;

I have tried setting the date_default_timezone_set('UTC') but this didn't worked for me.

Any idea for this weird behavior?

Note : For PHP Info of my server

Ravi Soni
  • 2,210
  • 3
  • 31
  • 53
  • may be help you this question: http://stackoverflow.com/questions/12985413/server-strtotime-incorrect – Bora Sep 10 '13 at 06:59
  • did you check the value of $listdate, I mean if its getting replaced in correct format before passing it in date function – Deepanshu Goyal Sep 10 '13 at 07:05
  • @Deepanshu Yes i have checked that already. – Ravi Soni Sep 10 '13 at 07:06
  • @ravisoni Your code checked out for me. Are you absolutely positive that what you posted is in fact the actual code you are using? – Funk Forty Niner Sep 10 '13 at 07:12
  • I am getting the $str from scraping this site : http://breitlingsource.com/phpBB2/viewtopic.php?f=3&t=49531&sid=1b260cff7a9f30f6a11c9a14385b7be2 and i wrote the string after echoing it. – Ravi Soni Sep 10 '13 at 07:16
  • During scraping, you strip the html tags and everything, right? – EPB Sep 10 '13 at 07:23
  • @ravisoni *"...from scraping this site..."* - Well you're not going to achieve it with what you posted for code. No wonder you're not getting the actual date. Obviously that's not your full code then. This is probably due to the two different clocks, theirs and yours. Since you're trying to grab the time from two different servers, they're not synchronized. – Funk Forty Niner Sep 10 '13 at 07:25
  • I have escaped all tag with strip_tags and decoded the string too, I have posted the copied string as i can see in the browser source. This is really frustrating :( – Ravi Soni Sep 10 '13 at 08:33

1 Answers1

0

Have you setted the date_default_timezone_set BEFORE the script?

As suggested on the php documentation:

To avoid frustrating confusion I recommend always calling date_default_timezone_set('UTC') before using strtotime().

Because the UNIX Epoch is always in UTC; you will most likely output the wrong time if you do not do this.

Cheers

If you did it, maybe can you check what does output, in general, another date?

E.G.:

$listdate = date("Y-m-d",strtotime("now"));

Also, does mktime work for you?

EDIT after comments:

It seems that you're not the only one experiencing such problems with PHP 5.3, in fact, always from the documentation, a user experienced something similar and gave a solution for windows platforms:

Most of the scripts here depend on PHP identifying the correct zone when one is not set, but that does not always happen.

For example PHP 5.3.3 gives a 'UTC' result for a detected '8.0/no DST'. Not much help.

So for those of us who for very good reasons really cannot set time zones in scripts of in .ini files, here is a function for use in a Windows environment that sets the default time zone to the OS time zone and returns the detected and set texts.

<?php 
function date_system_timezone_set(){ 
  $shell = new COM("WScript.Shell") or die("Requires Windows Scripting Host"); 
  $time_bias = -($shell->RegRead( 
"HKEY_LOCAL_MACHINE\\SYSTEM\\CurrentControlSet\\Control 
\\TimeZoneInformation\\Bias"))/60; 
  $ab = -($shell->RegRead( 
"HKEY_LOCAL_MACHINE\\SYSTEM\\CurrentControlSet\\Control 
\\TimeZoneInformation\\ActiveTimeBias"))/60; 
  $sc = $shell->RegRead( 
"HKEY_USERS\\.DEFAULT\\Control Panel\\International\\sCountry"); 
  foreach(timezone_abbreviations_list() as $tza) foreach($tza as $entry){ 
    $country = strtok($entry['timezone_id'],'/'); 
    $locale = strtok('|'); 
    if($country==$sc && $ab==($entry['offset']/60/60) && ($ds = $time_bias!=$ab)==$entry['dst']){ 
      date_default_timezone_set($timezone_identifier = $country."/".$locale); 
      return sprintf('%.1f',$ab)."/".($ds?'':'no ').'DST'." ".$timezone_identifier; 
     } 
   } 
  return false; 
 } 
?>

Also, looking on googles, after maaaaaaaaaany searchs, I found out many persons saying that in PHP 5+ you MUST define date.timezone in PHP.ini!

From a forum:

date.timezone option in php.ini must be set in PHP 5.3.0+

link to the topi: http://www.silverstripe.org/installing-silverstripe/show/15398?start=8

So, please try to add to your PHP.ini date.timezone!

Other update (always after more comments):

Since your code is working if you parse the date, use a date parser or try this:

<?php
$str = 'Posted: Thu Sep 05 2013 3:40 pm ';
$listdate = preg_replace('/Posted: /','',$str );
$listdate = date('Y-m-d',strtotime($listdate)); 
echo $listdate;
?>

This echos to me 2013-09-05

If this still doesn't work use datetime or date parsers as suggested here...

Last and FINAL edit:

Tried this code on my crunchbang linux with php 5 and it actually works:

<?php
$str = 'Posted: Thu Sep 05, 2013 3:40 pm ';
$listdate = preg_replace('/Posted: /','',$str );
$date = date_create_from_format('D M j, Y g:i a ', $listdate);
echo date_format($date, 'Y-m-d');
?>
Community
  • 1
  • 1
briosheje
  • 7,356
  • 2
  • 32
  • 54
  • I have tried date_default_timezone_set('UTC') very first when i trap in this situation but this is not helping. – Ravi Soni Sep 10 '13 at 08:40
  • Ok, trying in my pc, in windows, your function actually work. However, check this out: since you're using PHP 5, try to change your PHP.ini configuration. From the documentation: "Instead of using this function (date_default_timezone_set) to set the default timezone in your script, you can also use the INI setting date.timezone to set the default timezone.". Check this out: http://www.php.net/manual/en/datetime.configuration.php#ini.date.timezone . So, try to change timezone from your PHP.ini file and please tell us if that work, maybe this will fix it! – briosheje Sep 10 '13 at 08:59
  • Moreover, check this out too: http://stackoverflow.com/questions/10289214/converting-php4-date-to-php5-date-format. Maybe your problem is related both on your PHP version and your server! – briosheje Sep 10 '13 at 09:02
  • when i simply use this string `Thu Sep 05, 2013 3:40 pm` this works perfectly without setting any timezone. – Ravi Soni Sep 10 '13 at 09:15
  • Interesting. What does $listdate = preg_replace('/Posted: /','',$str ); output to you? have you tried to trim it? : $listdate = trim(preg_replace('/Posted: /','',$str)); Also, strotime($listdate) should be 1378388400! – briosheje Sep 10 '13 at 09:18
  • I have tried trimming and strotime($listdate) is giving just a 0 – Ravi Soni Sep 10 '13 at 09:33
  • So at least we know that the problem is in strtotime OR in $listdate. what's the echo of your $listdate? It should be "Thu Sep 05, 2013 3:40 pm". Also, try strtotime($listdate) without trimming and see if the result is the same. If the output is "Thu Sep 05, 2013 3:40" then try to do this: $listdate = str_replace('Posted: ','',$listdate); and see if strtotime($listdate) is different.. – briosheje Sep 10 '13 at 09:34
  • `echo $listed;` output's `Thu Sep 05, 2013 3:40 pm ` – Ravi Soni Sep 10 '13 at 09:37
  • Ok so, unless there is something absolutely wrong in the other pieces of your code there is something wrong with $listed. Have you tried to use str_replace instead of preg_replace? – briosheje Sep 10 '13 at 09:42
  • Update: check also this: http://stackoverflow.com/questions/5680056/php-strtotime-not-working this seriously may solve your issue! – briosheje Sep 10 '13 at 09:49
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/37092/discussion-between-briosheje-and-ravisoni) – briosheje Sep 10 '13 at 10:16