2

im passing a startDate end an endDate as a get request parameter to a method, here they get parsed like :

$startDate=$request->query->get('start');
$endDate=$request->query->get('end');
$logger->info('startdate is :'.$startDate.', endDate is : '.$endDate.'');

$start=new \DateTime($startDate);
$end=new \DateTime($endDate); 

when i log those two parameters, they may be

startdate is: Wed Jan 12 2011 00:00:00 GMT 0100 (CET)

startDate is: Sat Jan 12 2013 00:00:00 GMT 0100 (CET)

so far so good, but if i log the DateTime´s instanciated from the string above it returns

DateTime Object ( [date] => 0100-01-12 00:00:00 [timezone_type] => 2 [timezone] => GMT ) 

DateTime Object ( [date] => 0100-01-15 00:00:00 [timezone_type] => 2 [timezone] => GMT )

you can see, the DateTime does not represent the same Date

can i make a valid DateTime from those Strings ?

Update :

i tryed to use createFromFormat

like

    $startDate=$request->query->get('start');
    $endDate=$request->query->get('end');

    $start=new \DateTime::createFromFormat('D M d Y h:i:s e+O (T)',$startDate);
    $end=new \DateTime::createFromFormat('D M d Y h:i:s e+O (T)',$endDate);   

but that causes exception :

FatalErrorException: Parse: syntax error, unexpected 'createFromFormat' (T_STRING), expecting variable (T_VARIABLE) or '$' in 

i also tryed :

    $start=new \DateTime(\DateTime::createFromFormat('D M d Y h:i:s e+O (T)',$startDate));
    $end=new \DateTime(\DateTime::createFromFormat('D M d Y h:i:s e+O (T)',$endDate));   

But that creates Dates a new Date from right now ( 2014-01-21 12:28:57 )

I just dont get it right.

for any help, thanks in advance!

1
  • Just for documentation, always trim your timestamps. I had a space in the end and got errors when using DateTime::createFromFormat(). Commented Sep 13, 2016 at 11:10

2 Answers 2

3

Your input datetime string Wed Jan 12 2011 00:00:00 GMT 0100 (CET) is not valid/standard for use in DateTime() or strtotime(). See date_parse() function to see how your datetime string is being parsed:

print_r( date_parse('Wed Jan 12 2011 00:00:00 GMT 0100 (CET)') );

demo

Use DateTime::createFromFormat() static method to return DateTime object according to the specific format.

demo

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

4 Comments

thanks a lot, is there a way to manipulate the datestring, so it can be parsed ? unfortunatly i cant change the dateString before passing into that method, or how would the "createFromFormat()" look for my case ? sorry im a complete newbie with date´s
@johnSmith: like I said, use DateTime::createFromFormat(). I have also given you a demo...
hmn i really dont get it soorry
@johnSmith: You can't even copy/paste my demo? Where do you see that I create DateTime object with DateTime object as first parameter? Static method DateTime::createFromFormat() already creates object, if you have valid format or input, otherwise false is returned. Can you see your mistake on this demo?
1

The date format you are probably using is RFC2822. As shown on the PHP date() page as this: Thu, 21 Dec 2000 16:01:07 +0200

You switched the month and day parts and PHP was unable to determine the correct parts.

Best practice would be to either use a Unix-Timestamp (seconds after Epoch) or a better format like ISO 8601 (2004-02-12T15:19:21+00:00).

2 Comments

hey, thanks. How can i do this ? I cant change the DateString before it comes into the method. I tryed some stuff with createFromFormat but that doesnt work, you have an idea ?
You should change this in your sending application. Can you control the sender? Maybe you give us some more Info on the sender? I would not try to tinker with the date string before parsing it, this will only lead to more problems. Rule of thumb is, if PHP cant parse your date, you shouldnt either.

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.