I have the following date: 2010-04-19 18:31:27. I would like to convert this date to the dd/mm/yyyy format.
You can use a regular expression or some manual string fiddling, but I think I prefer:
date("d/m/Y", strtotime($str));
<?php
$test1='2010-04-19 18:31:27';
echo date('d/m/Y',strtotime($test1));
?>
try this
If your date is in the format of a string use the explode function
array explode ( string $delimiter , string $string [, int $limit ] )
//In the case of your code
$length = strrpos($oldDate," ");
$newDate = explode( "-" , substr($oldDate,$length));
$output = $newDate[2]."/".$newDate[1]."/".$newDate[0];
Hope the above works now
There is also the DateTime object if you want to go that way: http://www.php.net/manual/en/datetime.construct.php
Simply Try
$date=date('d/m/Y',strtotime($yourdatevariable))
or else you want to convert to d-m-Y
Just Simply Edit above d/m/Y with d-m-Y
Try this:
$old_date = Date_create("2010-04-19 18:31:27");
$new_date = Date_format($old_date, "d/m/Y");
$source = 'your varible name';
$date = new DateTime($source);
$_REQUEST["date"] = $date->format('d-m-Y');
echo $_REQUEST["date"];
Related
I am using this function to change date format.
echo date("mdy", strtotime('2013-11-04'));
It's return: 110413
Now i want to decode it.
How to get date like this format: 2013-11-04
Remember 110413 should be as a input of that function
Thanks
Try like this
<?php
$MyDate="110413";
$date = DateTime::createFromFormat('mdy',$MyDate);
echo $newformat=$date->format('Y-m-d');
?>
$mydate=date("Y-m-d", strtotime('2013-11-04'));
echo date("Y-m-d", strtotime($mydate));
try like this,
$date = date("mdy", strtotime("2013-11-04"));
$myDateTime = DateTime::createFromFormat('mdy', $date);
$newDateString = $myDateTime->format('Y-m-d');
echo $newDateString;
I have a date which is formatted simply as 19830210 (YYYYDDMM).
How would I reorder it to 02101983 (DDMMYYYY) using php?
I simply want to rearrange the digits in the date.
use createFromFormat(), do:
$date = "19830210";
$newFormat = DateTime::createFromFormat('Ydm', $date);
echo $newFormat->format('dmY'); //will give you 02101983
echo $output=date('dmY',strtotime('02101983'));
For future use with date format use below link for reference
http://php.net/manual/en/function.date.php
use substr() function:
<?php
$date1 = 'YYYYDDMM';
$date2 = substr($date1 , 4 , 4).substr($date1 , 0 , 4);
echo $date2;
?>
$str='19830210'; //(YYYYDDMM).
echo $str."<br/>";
$yy=substr($str,0,4);
$mm=substr($str,4,2);
$dd=substr($str,6,2);
$reorder=$dd.$mm.$yy;
echo $reorder; //(DDMMYYYY).
I tried the following code to change the dateformat from dmy to ymd, but when using i got wrong dates.
My code
$sdate11=date("Y-m-d", strtotime($_POST["txtstartdates"]) );
$sdate111=date("Y-m-d", strtotime($_POST["txtenddates"]) );
dates inserted were
30-05-2013 and 31-05-2013
the date it returned was
2035-11-03 and 2036-11-02
could you please help me to find what was the problem here and solve it
Thank you.
Try with split like
$a = split('-',$_POST["txtstartdates"]);
or you can use explode even like
$a = explode('-',$_POST["txtstartdates"]);
$my_new_date = $a[2].'-'.$a[1].'-'.$a[0];
Here strtotime will not work for the format dd-mm-yyyy
You might use DateTime for that:
$date = DateTime::createFromFormat('d-m-Y', $_POST['txtstartdates']);
echo $date->format('Y-m-d');
$date = DateTime::createFromFormat('d-m-Y', $_POST['txtenddates']);
echo $date->format('Y-m-d');
Can't you use the DateTime object to convert the date into the format you want?
$DateTime = new DateTime($_POST['FIELD']);
echo $DateTime->format('Y-m-d');
Your code seems correct , I dont know why its not working for you Can you try below
I have made the '' from "" only
$sdate11=date('Y-m-d', strtotime($_POST['txtstartdates']));
$sdate111=date('Y-m-d', strtotime($_POST['txtenddates']));
You can do it using single line as:
$show_date = DateTime::createFromFormat('d-m-Y', $dateInput)->format('Y-m-d');
I've some data which formatted like: 04.09.1953
I want to convert this format to: 1953-09-04
Is there any way or php function to do this?
just use strtotime() to get a timestamp and then date() to convert that timestamp to the format you need:
$timestamp = strtotime("04.09.1953");
echo date("Y-m-d", $timestamp);
EDIT:
If you're having some "exotic" format as input, you might need to use explode(), list() and mktime() to build the timestamp on your own:
list($y,$m,$d) = explode(".","04.09.1953");
$timestamp = mktime(0,0,0,$m,$d,$y);
echo date("Y-m-d", $timestamp);
Have you tried strtotime() ? It might work, else you'll need to do manual conversion using substrings or explodes.
http://php.net/strtotime
http://php.net/substring
http://php.net/explode
If all you are interested in is converting from one "string" format to another you can use a regEx:
$DMY = '04.09.1953';
$YMD = preg_replace('/(\d\d).(\d\d).(\d{4,4})/', '$3-$2-$1', $DMY);
http://www.handyphp.com/index.php/PHP-Resources/Handy-PHP-Functions/reformat_date.html
function reformat_date($date, $format){
$output = date($format, strtotime($date));
return $output;
}
How can I convert this string 05/Feb/2010:14:00:01 to unixtime ?
Use the strtotime function:
Example:
$date = "25 december 2009";
$my_date = date('m/d/y', strtotime($date));
echo $my_date;
For PHP 5.3 this should work. You may need to fiddle with passing $dateInfo['is_dst'], wasn't working for me anyhow.
$date = '05/Feb/2010:14:00:01';
$dateInfo = date_parse_from_format('d/M/Y:H:i:s', $date);
$unixTimestamp = mktime(
$dateInfo['hour'], $dateInfo['minute'], $dateInfo['second'],
$dateInfo['month'], $dateInfo['day'], $dateInfo['year'],
$dateInfo['is_dst']
);
Versions prior, this should work.
$date = '05/Feb/2010:14:00:01';
$format = '#^(?P<day>\d{2})/(?P<month>[A-Z][a-z]{2})/(?P<year>\d{4}):(?P<hour>\d{2}):(?P<minute>\d{2}):(?P<second>\d{2})$#';
preg_match($format, $date, $dateInfo);
$unixTimestamp = mktime(
$dateInfo['hour'], $dateInfo['minute'], $dateInfo['second'],
date('n', strtotime($dateInfo['month'])), $dateInfo['day'], $dateInfo['year'],
date('I')
);
You may not like regular expressions. You could annotate it, of course, but not everyone likes that either. So, this is an alternative.
$day = $date[0].$date[1];
$month = date('n', strtotime($date[3].$date[4].$date[5]));
$year = $date[7].$date[8].$date[9].$date[10];
$hour = $date[12].$date[13];
$minute = $date[15].$date[16];
$second = $date[18].$date[19];
Or substr, or explode, whatever you wish to parse that string.
You should look into the strtotime() function.
http://www.php.net/date_parse_from_format
$d="05/Feb/2010:14:00:01";
$dr= date_create_from_format('d/M/Y:H:i:s', $d);
echo $dr->format('Y-m-d H:i:s');
here you get date string, give format specifier in ->format() according to format needed
Simple exploding should do the trick:
$monthNamesToInt = array('Jan'=>1,'Feb'=>2, 'Mar'=>3 /*, [...]*/ );
$datetime = '05/Feb/2010:14:00:01';
list($date,$hour,$minute,$second) = explode(':',$datetime);
list($day,$month,$year) = explode('/',$date);
$unixtime = mktime((int)$hour, (int)$minute, (int)$second, $monthNamesToInt[$month], (int)$day, (int)$year);
If you're up for it, use the DateTime class
Try this:
$new_date=date('d-m-Y', strtotime($date));
If it's a string that you trust meaning that you have checked it before hand then the following would also work.
$date = new DateTime('2015-03-27');