This question already has answers here:
Convert a date format in PHP [duplicate]
(18 answers)
Closed 4 years ago.
I have an array index that comes from an external API that returns a date in a string like this:
<?php echo gettype($meeting['date']); ?>
Results in "string".
This:
<?php echo $meeting['date']; ?>
Gives the following outcome: "2018-08-30".
I want to change the date format to "30-08-2018".
How can i accomplish this?
You can easily do this using DateTime class and the format() method:
// Create a DateTime instance
$date = new DateTime($meeting['date']);
// Format it:
echo $date->format('d-m-Y');
// Output: 30-08-2018
$newDate = date("d-m-Y", strtotime($meeting['date']));
echo $newDate;
That's done.
$meeting['date']="2018-08-30";
echo date("d-m-Y", strtotime($meeting['date']));
The above code will do the job for you. It will get your string date and convert it to the format you need.
The output of the above code is : 30-08-2018
More about date function you can find here!!!
Use strtotime() and date():
$originalDate = "2018-08-30";
$newDate = date("d-m-Y", strtotime($originalDate));
Related
This question already has answers here:
Convert one date format into another in PHP
(17 answers)
Closed 6 years ago.
I need to convert date format in php , But i am getting getting error
here is my code
$test = new DateTime('23/09/2016');
echo date_format($test, 'Y-m-d');
But i am getting error as
Message: DateTime::__construct(): Failed to parse time string (23/09/2016) at position 0 (2): Unexpected character
How to resolve the issue
You're not telling DateTime that what's your string date format. So, it's unable to create a DateTime object.
This should work :
$test = DateTime::createFromFormat('d/m/Y', '23/09/2016');
echo date_format($test, 'Y-m-d');
Read more about createFromFormat here.
Use createFromFormat instead. Like this,
$date = DateTime::createFromFormat('d/m/Y', '23/09/2016');
echo date_format($date, 'Y-m-d');
It's throwing exception because it's unable to parse String to date with / in it.
http://php.net/manual/en/datetime.createfromformat.php
You are getting error because your code is not match with Years and Day.
If you write code below it will work.I just exchange between Years and days .
$test = new DateTime('2016/09/23');
echo date_format($test, 'Y-m-d');
Or you can use createFromFormat to format first date string then convert it in your required format.
$input = "23/09/2016";
$arr = explode("/", $input);
# mktime is a function to create timestamp
# mktime (hour, min, sec, month, day, year)
$mktime = mktime(0,0,0,(int) $arr[1],(int)$arr[0],(int)$arr[2]);
$date1 = date ('d/m/Y', $mktime);
$date2 = date ('Y/m/d', $mktime);
echo "input: $input\ndate1: $date1\ndate2: $date2\n";
or you can try this one, $date2 is the final result.
This question already has answers here:
Convert one date format into another in PHP
(17 answers)
how to re-format datetime string in php?
(9 answers)
Closed 6 years ago.
I have some strings that will be different dates and times. Each one will be different and I'm looking to format them to my liking.
For Example:
2016-04-15 14:20:00
I would like to be able to take this string and use PHP's Date function to format it to my liking.
For Example:
04/15 2:20pm
So would I take the string and convert it to a timestamp first? If so how do I go about doing that?
You can return a new DateTime object formatted according to the specified format by using this function. Then you can format it as you like.
<?php
$testDate = '15-Feb-2009 11:00:34';
$date = DateTime::createFromFormat('j-M-Y H:i:s', $testDate);
echo $date->format('Y-m-d');
?>
try this ..
<?php
$time = strtotime('2016-04-15 14:20:00');
echo date('m/d g:ia', $time);
?>
Second Way
<?php
$format = 'Y-m-d H:i:s';
$date = DateTime::createFromFormat($format, '2016-04-15 14:20:00');
echo $date->format('m/d g:ia');
?>
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');
?>
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:
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));