This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Strtotime() doesn’t work with dd/mm/YYYY format
I have this variable which i get the info like this:
echo $start=$_REQUEST['to'];
It outputs something like this:
2/04/2012
What i need is to convert it like this: 20120402
Could you please help me? I tried strotime and no success..
I tried converting the string before in a date format, then i converted it in a Ymd format, but i kept receiving a strange date, something like 1970 !
I tried this:
$time = strtotime( $date );
$myDate = date( 'y-m-d', $time );
thanks!
It may work.
<?php
$start=$_REQUEST['to'];
$date = explode("/",$start);
$size = sizeof($date);
for($i=$size;$i>=0;$i--) {
$date_get .= $date[$i];
}
echo $date_get;
?>
You should use strftime instead of date.
$myDate = strftime('%Y%m%d', $time);
Related
This question already has answers here:
Convert one date format into another in PHP
(17 answers)
Closed 4 years ago.
I'm trying to convert a date and time variable into date only in a different format:
$test_date = '01/07/2018 10:00-12:00';
$result = substr($test_date, 0, 10);
$newDate = date("Ymd", strtotime($result));
echo $newDate;
Expected result= 20180701
Actual result= 20180107
Tried all the relative variations I can find on here but keep getting 'Ydm' instead of 'Ymd'.
I'm I missing something obvious here?
Looks like it's being parsed 'wrongly' because it's not explicitly stated what the source format is, using DateTime objects, you can do (not tested):
$result = substr($test_date, 0, 10);
$dateTime = DateTime::createFromFormat('d/m/Y', $result);
$newDate = $dateTime->format('Y-m-d');
echo $newDate;
This question already has answers here:
Convert one date format into another in PHP
(17 answers)
Closed 6 years ago.
I have to convert this string date 2016-09-26 00:00:00.000 to the yyyy-mm-dd format without other characters.
Can you help me, please?
You can just use the DateTime class along with the format() method:
$d = new DateTime('2016-09-26 00:00:00.000');
echo $d->format('Y-m-d');
Try this:
$datetime = '2016-09-26 00:00:00.000';
$date = date('Y-m-d', strtotime('2016-09-26 00:00:00.000'));
you will get the only date part in 'yyyy-mm-dd' format.
$test = strtotime('2016-09-26 00:00:00.000');
$test1 = date('Y-m-d h:i', $test);
$array1 = explode(' ', $var);
$date_temp = $array1[0];
This question already has answers here:
Convert one date format into another in PHP
(17 answers)
Closed 7 years ago.
I have date string like:
08.2015
What I want to do is, to convert it into MySQL compatible date format:
date("Y-m-d", strtotime(date_create_from_format('m.Y', "08.2015")));
Doesn't work as expected. What am I doing wrong?
BTW: It's okay if I'll get 2015-06-01 result.
You don't need strtotime there.
echo DateTime::createFromFormat('m.Y', "08.2015")->format("Y-m-d");
Output: 2015-08-31
Fiddle
And if you always want to get first date of month with only m.Y given, you can do
echo DateTime::createFromFormat('d.m.Y', "01."."08.2015")->format("Y-m-d");
Output: 2015-08-01
Fiddle
<?php
//explode the date
$dateProps = explode('.',"08.2015");
//prepare the format
$date = $dateProps[1].'-'.$dateProps[0].'-01';
//use date functions
$date = date('Y-m-d', strtotime($date));
Try this.
$var = '08.2015';
$date = '01-'.str_replace('.', '-', $var);
echo date('Y-m-d', strtotime($date));
I think below SQL useful to you.
SELECT DATE_FORMAT(STR_TO_DATE('06.2015','%m.%Y'),'%Y-%m');
output: 2015-06
Thank you.
First of all, if you want to use strtotime , it should be this : strtotime('01.08.2015'), that is to say , Year month day must be all needed.
You can try below sql-
SELECT DATE_FORMAT(STR_TO_DATE('08.2015','%m.%Y'),'%Y-%m-01')
This question already has answers here:
Strtotime() doesn't work with dd/mm/YYYY format
(17 answers)
Closed 7 years ago.
I have a string value "27/03/2015" and i want to convert this string to new date format. Below is the code i am using now.
<?php echo date("Y-m-d",strtotime("27/03/2015")); ?>
But it gives a wrong output like this 1970-01-01.
It is because strtotime isn't being able to parse your date string. Try:
<?php echo strtotime("27/03/2015"); ?>
The result should be False. Since False is the same as 0, you are really running date("Y-m-d", 0), the result of which is "1970-01-01" (the "unix epoch").
strtotime only recognizes certain date formats, listed here. The closest to your input format is "27-03-2015" ("Day, month and four digit year, with dots, tabs or dashes").
try this
<?php echo date("Y-m-d",strtotime(str_replace('/', '-', YOUR DATE )))); ?>
Here is the simple solution
$date = '27/03/2015';
$date = str_replace('/', '-', $date);
echo date('Y-m-d', strtotime($date));
in above case / Seperator is not valid (as the date will be evaluated to Date 3 & Month 27)
you can use -
echo date("Y-m-d",strtotime("27-03-2015"));
I guess "/" is not allowed or, should I say, unrecognizable as a parameter for strtotime.
<?php
$dateString = "27/03/2015";
//now let's check if the variable has a "/" inside of it.
//If it does, then replace "/" with "-".
//If it doesnt, then go with it.
//"." also accepted for strtotime as well.
$dateString = (strpos($dateString,"/") ? str_replace("/","-",$dateString) : $dateString);
echo date("Y-m-d",strtotime($dateString));
?>
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Convert timestamp to readable date/time PHP
Given 1328520572 as a datetime string,
how can i get the datetime in specific format from it ?
I'd like to use
str = '1328520572';
str_ret=datetime.Parse("M:D:Y H:M:S",str);
$str = '1328520572';
echo date("M:D:Y H:M:S", (int)$str);
But your date format is wrong, check the right format here.
Properly formatted datetime?
$unix = '1328520572';
$datetime = date('Y-m-d H:i:s', $unix);
$str_ret = date("m:d:Y H:i:s", $str);
Try this:
$str = '1328520572';
$str_ret = date('M:D:Y H:M:S', strtotime($str));