This question already has answers here:
Convert one date format into another in PHP
(17 answers)
Closed 3 years ago.
I have a simple section in which a user can select a range of date.
PHP to get these dates from datepicker
if (isset($_POST['from_date']) && isset($_POST['to_date'])) {
$firstDate= $_POST['from_date'];
$lastDate= $_POST['to_date'];
var_dump($firstDate);
var_dump($lastDate);
}
From the above PHP script, I am getting this
string(10) "08/13/2019"
string(10) "08/14/2019"
But I want this
string(10) "2019-08-13"
string(10) "2019-08-14"
What do I need to do to achieve what I want?
You can use strtotime() :
$time = strtotime('08/13/2019');
$newformat = date('Y-m-d',$time);
echo $newformat;
// 2019-08-13
Related
This question already has answers here:
Convert one date format into another in PHP
(17 answers)
Closed 4 years ago.
I have the following date: 2010-04-19 .
I would like to convert this date strtotime format and convert "Y-m" format.
Example = "2018-5"
my code:
$date1= 'onlinearticles_'.$date;
$article_table= date('Y-m',strtotime($date1));
But it didin't work, how can I do this ?
you should add alphabetic characters later..
follow these steps;
$date = 2010-04-19;
$newDate = date("Y-m",strtotime($date));
$stringWithDate = "onlinearticles_".$newDate;
dd($stringWithDate);
This question already has answers here:
Convert one date format into another in PHP
(17 answers)
Closed 6 years ago.
I have a String on the form: "dd/mm-yyyy", where dd is day of the month with two digits, mm is the month represented with two digits, and yyyy is the year with four digits.
I need to store this in my database. Ive tried
$dato = date('d/m-Y' ,strtotime($_POST['dag'] )
But that clearly doesnt work. In my database the date displays as yyyy-mm-dd. How do I properly convert the String to the correct format?
strtotime not accept your time string format, so it return false. You can use DateTime::createFromFormat or date_create_from_format, manual here.
DateTime::createFromFormat('d/m-Y', $_POST['dag']);
check the live demo
<?php
var_dump(DateTime::createFromFormat('d/m-Y', '11/01-2017'));
putput:
object(DateTime)#1 (3) {
["date"]=>
string(26) "2017-01-11 14:34:53.000000"
["timezone_type"]=>
int(3)
["timezone"]=>
string(3) "UTC"
}
Try to replace the / with a - like so:
$date = "02/04-2016";
$dateNew = str_replace("/","-",$date);
You can use DateTime::createFromFormat:
$time = '12/3-1987';
$data = DateTime::createFromFormat('d/m-Y', $time);
echo $data->format('Y-m-d'); //1987-03-12
sandbox
This question already has answers here:
Convert one date format into another in PHP
(17 answers)
Closed 6 years ago.
i've a device that give me file logs with date format like this: 16/11/07 (today date) but when i'm trying to convert it with date() and strtotime, doesn't work and give me 1970-01-01.
$row2 = "16/11/07;11:49:00";
$par = explode(";",$row2);
$date = date("Y-m-d", strtotime($par[0]));
Do you have some ideas?
I don't want to create a script with "20" in front of that date, because is not well formed coded.
Thank you
Use DateTime object.
try{
$date = DateTime::createFromFormat('y/m/d',$my_date);
$req_date = $date->format('Y-m-d');
echo "Req Date : ".$req_date;
} catch (Exception $e) {
echo $e->getMessage();
}
This question already has answers here:
Convert one date format into another in PHP
(17 answers)
Closed 6 years ago.
I need to convert string value "290416" which is actually date but not in correct format. I need to change it in date format like 29/04/16.
please help.
If you don't need it as a date but only in date format. Meaning you are not performing any date-functions on it but just displaying it as a date you could use
$str = '290416';
$arr = str_split($str, 2);
$date_string = $implode('/', $arr);
The most robust way will be to use createFromFormat, passing in your format and the string, and they you have a DateTime object and can do many things with it.
define('MY_DATE_INPUT_FORMAT', 'mdy');
define('MY_DATE_OUTPUT_FORMAT', 'm/d/y');
$inputDateString = '042916';
$dateObj = DateTime::createFromFormat(MY_DATE_INPUT_FORMAT, $inputDateString);
$outputString = $dateObj->format(MY_DATE_OUTPUT_FORMAT);
This can also be done procedurally:
$date = date_create_from_format(MY_DATE_INPUT_FORMAT, $inputDateString);
echo date_format($date, MY_DATE_OUTPUT_FORMAT);
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');
?>