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

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'));?>

Related

How to get date("Y-m-d") from date("Y-m-d H:i:s")? [duplicate]

This question already has answers here:
Convert one date format into another in PHP
(17 answers)
Closed 6 years ago.
how can I get and store date('2017-02-02') in php variable from result : datetime("2017-02-02 17:02:03").
Do you mean getting 2017-02-02 from 2017-02-02 17:02:03, well you have many options,
$date = date('Y-m-d', strtotime('2017-02-02 17:02:03'));
OR
list($date) = explode(" ", '2017-02-02 17:02:03');
OR
$arr = explode(" ", '2017-02-02 17:02:03');
$date = array_shift($arr);
OR
$dateObj = DateTime::createFromFormat("Y-m-d H:i:s", '2017-02-02 17:02:03');
$date = $dateObj->format("Y-m-d");
You need to convert the date to a timestamp with strtotime()
Example:
<?php
echo date("Y-m-d", strtotime('2017-02-02 17:02:03'));
?>
I hope this will help you!

Extract hour information from a date string [duplicate]

This question already has answers here:
Convert one date format into another in PHP
(17 answers)
Closed 7 years ago.
Is it possible extract hour value from a date string like below
20151211091805 +0000
expected output-
09:18:05 (this should also be string)
Try this:
$t = "20151211091805";
echo date('H:i:s',strtotime($t));
Use PHP's date function.
echo date("H:i:s", strtotime("20151211091805"));
Another way using DateTime class.
$dateTime = new DateTime("20151211091805");
echo $dateTime->format('H:i:s');
Try this..
<?php
$timestamp = 20151211091805;
$date = new DateTime("$ts");
echo date_format($date, 'H:i:s');
?>

How can I convert a string to date (php)? [duplicate]

This question already has answers here:
Convert one date format into another in PHP
(17 answers)
Closed 7 years ago.
I would like to convert 30.10.2014 15\:25\:24 to 30/Oct/2014 15:25:24. I tried it like this...
$s = '30.10.2014 15\:25\:24';
$date = strtotime($s);
echo date('d/M/Y H:i:s', $date);
... but my result is 01.Jan.1970 01:00:00
Its because strtotime returned FALSE , code shown below
$s = '30.10.2014 15\:25\:24'; //invalid format
$date = strtotime($s);
if($date===FALSE) //if strtotime failed
{
echo "strtotime failed";
}

Take PHP variable value of 201501 for example and display January 2015 [duplicate]

This question already has answers here:
Convert one date format into another in PHP
(17 answers)
Closed 8 years ago.
I have a date value that is being passed as 201501.
How could I take that and display January 2015 instead?
It's being passed through a PHP variable.
Just try with:
$input = '201501';
$output = DateTime::createFromFormat('Ym', $input)->format('F Y');
Try below code
$data = '201501';
$monthNum = substr($data,4);
$year = substr($data,0,4);
$dateObj = DateTime::createFromFormat('!m', $monthNum);
$monthName = $dateObj->format('F');
echo $monthName.' '.$year;

Convert string date to new format [duplicate]

This question already has answers here:
Convert one date format into another in PHP
(17 answers)
Closed 8 years ago.
How to convert a string, for instance
$datestring = '28.08.14 10.42';
To a new format like date("YmdHis.u")
Thank you
Use date function. There is no such format as what you have required but the what I think you want to achieve is this.
DEMO
<?php
$datestring = '28.08.14 10.42';
$date = date("Y-m-d H:i:s:u", strtotime($datestring));
echo $date; //outputs 2014-08-28 10:42:00:000000
?>
Try this
$datestring = '28.08.14 10.42';
echo date("YmdHis.u",strtotime($datestring));

Categories