0

I have a string with the value, 25 July 2014 15:01 and I'm trying to turn it into a timestamp.

This is the code that I'm trying to use, although it's not working, any ideas?

    $date = date_create_from_format('j F y G:i', $dateString);
    var_dump($date);

This is my code: https://www.dropbox.com/s/pg2o5piz45g3piw/Screenshot%202014-07-26%2019.03.22.png

And this is the output: https://www.dropbox.com/s/o5jr6ob23crsbf6/Screenshot%202014-07-26%2019.03.20.png

Fixed.

I am using the simple_html_dom module, and I was accidentally using outertext instead of innertext, so I was getting the span html as well!

1
  • 1
    Stack Overflow has an awesome markdown editor to post code (as well as inline pictures). There's no need to bother taking screenshots of your code and uploading them to external services—they are also not indexable and cannot be copied and pasted. Commented Jul 26, 2014 at 18:12

2 Answers 2

2

y (lowercase) denotes a two digit representation of the year - try uppercase (full year) instead:

$date = date_create_from_format('j F Y G:i', $dateString);
var_dump($date);

Outputs something like:

object(DateTime)#1 (3) {
  ["date"]=>
      string(19) "2014-07-25 15:01:00"
  ["timezone_type"]=>
      int(3)
  ["timezone"]=>
      string(13) "Europe/London"
}

You might want to check out the Date manual.

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

Comments

1

Try this:

$str = "25 July 2014 15:01";
echo strtotime($str);

strtotime() will automagically parse almost any string which has datetime information into an Unix timestamp. You can do nifty things like:

echo strtotime("+1 day");
echo strtotime("next Thursday");

:)

6 Comments

echo "new:" . strtotime($dateString); I added this, although nothing is coming up, nor is there anything in the PHP error log.
@HarryTorry - what is value of $dateString? Can you paste that?
See the original comment, I've added images!
@HarryTorry - when I run this $str = "25 July 2014 15:01"; echo strtotime($str); I get 1406293260. In your code, comment all the rest of the lines and see running just these ones first.
FYI, strtotime() uses the same heuristics that the DateTime constructor.
|

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.