Search code examples
phpregexstringstrip

PHP - Turn a timestamp (2012-07-12 20:26:00) Into two seperate variables that refelct the month and day values?


I would like to separately strip the day and month values out of this timestamp "2012-07-12 17:50:00".

So essentially I could have two variables like:

$month = 7
$day = 12

As I am not very proficient with php, specifically regex coding, I thought I would post this question to ask for advice as to how I would go about accomplishing something like this.

Any assistance, insight or input in this regard would be greatly appreciated. Thank you!

AS A NOTE: Futhermore, I would like to turn the two variables into an output like "12th July". This can be coded quite easily so I have not made this a part of the question but if there is a simple function that deals with this information would be much appreciated too!


Solution

  • <?php
    list($month,$day) = explode(':',date('n:j',strtotime('2012-07-12 17:50:00')),2);
    echo 'Month: '.$month.'<br />'."\n";//Month: 7
    echo 'Day: '.$day.'<br />'."\n";//Day: 12
    ?>
    

    And...

    <?php
    echo date('jS F',strtotime('2012-07-12 17:50:00'));//Outputs: 12th July
    ?>