Php get month number from string with date [duplicate] - php

This question already has answers here:
Get month of a given date
(6 answers)
Closed 7 years ago.
i have a string in this format '2015-4-28'
How i get month number for this string ( the value in this example is 4)?
Thanks

Another option would be to do this:
date('m', strtotime($date));
Change the m to an n to get the month without the leading 0.

With this:
<?php
$a = "2015-4-28";
$parts= explode("-", $a);
echo $parts[0]; // 2015
echo $parts[1]; // 4
echo $parts[2]; // 28
?>

Related

month with leading 0 getting wrong result from createfromformat [duplicate]

This question already has answers here:
DateTime converts wrong when system time is 30 March
(2 answers)
Closed 1 year ago.
I want to convert this date 2021-02 into 02-2021, so I did:
echo DateTime::createFromFormat('Y-m', '2021-02')->format('m/Y');
but this return 03/2021
and the correct result is 02/2021
you have to specify the day:
echo DateTime::createFromFormat('Y-m-d', '2021-02-someday')->format('m/Y');
for example:
echo DateTime::createFromFormat('Y-m-d', '2021-02-01')->format('m/Y');

php date() method is showing wrong date [duplicate]

This question already has an answer here:
strtotime() with only year return wrong data
(1 answer)
Closed 2 years ago.
I have this PHP code:
<?php
$date = 2022;
$str = strtotime(2022);
echo $str; // 1613161320
echo '<hr/>';
echo date('Y', $str); // 2021
why this link date('Y', $str); is showing 2021 instead of 2022 ?
strtotime() expects a formated date string like 10 September 2000 see php docs.
When you want to get a unix timestamp by seperate year, month, day, hour, minute and second values you should have a look at mktime() see php docs.
So what you want to get the first 2022-date possible is:
$str = mktime(0, 0, 0, 1, 1, 2022);

Get day number from Date format by php [duplicate]

This question already has answers here:
How to extract only 'Day' value from full 'Date' string?
(7 answers)
Closed 2 years ago.
I have a date Formate from database number 2020-11-15 I want to get only the day number. I tried the following.
$your_date = '2020-11-15';
$ar_day2 = date("D", strtotime($your_date)); //The value is (Sun)
I am looking for result '15'.
Is there way by php?
use small d
$ar_day2 = date("d", strtotime('2020-11-15'));
echo $ar_day2 = date("d", strtotime('2020-11-15')); // will give => 15
echo $ar_day2 = date("D", strtotime('2020-11-15')); // will give => Sun

PHP separate variable container [duplicate]

This question already has answers here:
Convert one date format into another in PHP
(17 answers)
Closed 5 years ago.
I have a variable that contains a date
$date = "04/18/2017 04:02 PM";
This comes from a date field, This field will always contain a date like this, and What I'm trying to do is separating the date from the time
So how do i go about getting only 04/18/2017 from the variable or the time 04:02 PM ?
Thanks.
Just do a simple explode:
$explode = explode($date, ' ', 2);
$date = $explode[0];
$time = $explode[1];

Converting month name in given date into month number [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
PHP date format converting
I have a date like 2012-september-09. How to convert this into 2012-09-09 using PHP?
You can try with strptime
$date = '2012-september-09';
$strp = strptime($date, '%Y-%B-%d');
$ymd = sprintf('%04d-%02d-%02d', $strp['tm_year'] + 1900, $strp['tm_mon'] + 1, $strp['tm_mday']);
$new_date = new DateTime($ymd);
echo $new_date->format('Y-m-d');
here's a Codepad
Try this
Change
<?php echo date('Y-m-d',strtotime('2012-september-09'));?>
To
<?php echo date('Y-m-d',strtotime('09-september-2012'));?>

Categories