Get month from timestamp php

Given the following timestring:

$str = '2000-11-29';

$php_date = getdate( $str );
echo '<pre>';
print_r ($php_date);
echo '</pre>';

How to get the year/month/day in PHP?

[seconds] => 20
[minutes] => 33
[hours] => 18
[mday] => 31
[wday] => 3
[mon] => 12
[year] => 1969
[yday] => 364
[weekday] => Wednesday
[month] => December
[0] => 2000

I don't know why I get 1969 for year.

Thank you

asked Aug 31, 2010 at 3:51

You can use strtotime to parse a time string, and pass the resulting timestamp to getdate (or use date to format your time).

$str = '2000-11-29';

if (($timestamp = strtotime($str)) !== false)
{
  $php_date = getdate($timestamp);
  // or if you want to output a date in year/month/day format:
  $date = date("Y/m/d", $timestamp); // see the date manual page for format options      
}
else
{
  echo 'invalid timestamp!';
}

Note that strtotime will return false if the time string is invalid or can't be parsed. When the timestamp you're trying to parse is invalid, you end up with the 1969-12-31 date you encountered before.

RRikesh

13.6k5 gold badges47 silver badges68 bronze badges

answered Aug 31, 2010 at 3:53

0

PHP - How to get year, month, day from time string

$dateValue = strtotime($q);                     

$yr = date("Y", $dateValue) ." "; 
$mon = date("m", $dateValue)." "; 
$date = date("d", $dateValue); 

Get month from timestamp php

user1438038

5,6776 gold badges57 silver badges85 bronze badges

answered Nov 6, 2015 at 10:13

Get month from timestamp php

1

Update: Forgot to add semicolon at end of first line, try this:

<?php       
$str = "2010-08-29"; // Missed semicolon here
$time = strtotime($str);

// You can now use date() functions with $time, like
$weekday = date("l", $time); // Wednesday or whatever date it is    
?>

Hopefully that will get you going!

Get month from timestamp php

Mike Purcell

19.5k10 gold badges51 silver badges86 bronze badges

answered Aug 31, 2010 at 3:54

Raphael CaixetaRaphael Caixeta

7,7649 gold badges50 silver badges76 bronze badges

Using the object-oriented programming style, you can do this with DateTime class

$dateFormat = 'Y-m-d';
$stringDate = '2000-11-29';
$date = DateTime::createFromFormat($dateFormat, $stringDate);

Then, you can decompose your date using the format() method

$year = $date->format('Y'); // returns a string

If you prefer the numeric format, instead of the string format, you can use the intval() function

$year = intval($date->format('Y')); // returns an integer

Here some formats that you can use

  • Y A full numeric representation of a year, 4 digits
  • m Month of the year, 2 digits with leading zeros
  • d Day of the month, 2 digits with leading zeros
  • H 24-hour format of an hour, 2 digits with leading zeros
  • i Minutes, 2 digits with leading zeros
  • s Seconds, 2 digits with leading zeros

Here the entire list of the formats that you can use : http://php.net/manual/en/function.date.php

answered Feb 1, 2015 at 12:14

Get month from timestamp php

TyphonTyphon

8061 gold badge12 silver badges19 bronze badges

PHP - we can get year, month, day from time string in many formats.

$dateFromDb = '2021-2-6 13:15:19';

if (($timestamp = strtotime($dateFromDb)) !== false) {
    // auto format full time
    $php_date = getdate($timestamp);
    echo '<br>';
    foreach($php_date as $key =>$value){
        echo $key.'='.$value.'<br>';
    }

    // manually format time
    echo'-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-<br>';
    echo '4 digit year =            '.date("Y", $timestamp).'<br>';
    echo '2 digit year =            '.date("y", $timestamp).'<br>';
    echo 'Month =                   '.date("m", $timestamp).'<br>';
    echo 'Day =                     '.date("d", $timestamp).'<br>';
    echo 'Time in 24 Hours Format = '.date("H", $timestamp).'<br>';
    echo 'Time in 12 Hours Format = '.date("h", $timestamp).'<br>';
    echo 'Minutes =                 '.date("i", $timestamp).'<br>';
    echo 'Seconds =                 '.date("s", $timestamp).'<br>';
    echo '-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_- <br>';
    // and we can combine format also.
    // and we can add '/' or ':' between year,month,days or anything we want.
    echo 'Time =  ' .date("H:i:s" ,$timestamp).'<br>';
    echo 'Date =  ' .date("d/m/Y" ,$timestamp).'<br>';

    } else {
        echo 'invalid date format.';
    }

answered Jan 15, 2021 at 3:27

1