Explode date in php [duplicate] - php

This question already has answers here:
Converting to date in PHP from yyyymmdd format
(5 answers)
Closed 7 years ago.
I have a date in my sql with the following format:
20150426
I want to convert it to format 26/05/2015 into a variable.
Which is the best way to do it?
Can I do it with explode?

Do in this manner----
<?php
$nextBse = date('d/m/Y',strtotime(20150426));
echo $nextBse;
?>
Output:-- 26/04/2015

Please follow this method
edit:
d means "day"
m means "month"
Y means "year"
so the format of date will be d/m/Y
and we have a date to convert a new format but our data is not full data
like day/month/year/hour/minute/second
there are day,month,year
and we need to convert full date
so we use strtotime($ourdate);
$dt="20150426";
echo date('d/m/Y',strtotime($dt));

Related

Convert date April/2019 to 04/2019 [duplicate]

This question already has answers here:
Convert one date format into another in PHP
(17 answers)
Closed 4 years ago.
Now I'm having a date like this, April/2019. I wanted to convert it into 04/2019 in php. How to resolve this?
date('m/Y', strtotime($_POST`['card_form_plan_start_date']))
What I Wanted - 04/2019
Actual Result - 01/1970
strtotime won't recognise April/2019 as a date. Instead, you can use date_create_from_format with the F/Y format, then reformat the output as m/Y:
echo date_create_from_format('F/Y', 'April/2019')->format('m/Y');
Output
04/2019
Demo on 3v4l.org
You need to replace / with -
date('m/Y', strtotime(str_replace('/','-',$_POST`['card_form_plan_start_date'])))

How to Convert Date String into Different format? [duplicate]

This question already has answers here:
Convert one date format into another in PHP
(17 answers)
Closed 5 years ago.
I have a date that gets sent from a database which comes in a string format of '2018-01-01'. What I want to do is convert it into a string of month and year. E.G. 'January 2018'.
What would be the best way of doing so using PHP?
Try using the strtotime function to turn your string into a timestamp and the date function with the F Y format to get what you want:
$date = "2018-01-01";
$formatted = date("F Y", strtotime($date));

PHP Convert Date/Time Format to another [duplicate]

This question already has answers here:
Convert one date format into another in PHP
(17 answers)
Closed 7 years ago.
I have a Date/time value as such;
2014-01-07T16:19:08Z
I wish to convert it to the format below using PHP
2015-03-21 02:12:01
I know I could use string replace twice over to remove the T and Z characters but doesn't seem right..
use strtotime() with date()
echo date('Y-m-d H:i:s',strtotime('2014-01-07T16:19:08Z'));
The date_format() function returns a date formatted according to the specified format.
$date=date_create("2014-01-07T16:19:08Z");
echo date_format($date,"Y-m-d H:i:s");
One more way is there
echo date('Y-m-d H:i:s',strtotime('2014-01-07T16:19:08Z'));
Update
The date_create() function returns a new DateTime object.
Reference
<?php
echo date('Y-m-d H:i:s',strtotime('2014-01-07T16:19:08Z'));
?>

Time Format : PHP [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Covert time format in php
I am getting a start_time field value from mysql data base as 2012-08-14 21:30:00
and i want to convert it in php format like 2012-08-14T09:30 is there any method to do
this in php ?
use strtotime()
$date = strtotime('2012-08-14 21:30:00');
echo date('Y-m-d\Th:i',$date);
see this example.for the required date & time format:
$date=date("Y-m-d H:i:s");//get the current date
$d=strtotime($date);//convert in strtotime
echo $final_date=date("Y-m-d\Th:i:s",$d);//in the first argument of date ,put the format whatever you want,but be sure to convert it in strtotime first.

php converting date to YYYY-MM-DD hh:mm:ssTZD [duplicate]

This question already has answers here:
Convert one date format into another in PHP
(17 answers)
Closed 2 years ago.
I am tring to make the google news sitemap by php script from mysql.(all the date save as timestamp in +08:00)
But how to converting date to YYYY-MM-DD hh:mm:ssTZD?
For example 1338048000 => 2012-05-26T09:00:00+08:00
echo date("Y-m-d T h:i:s",'1338048000').'+08:00';//2012-05-26 PDT 09:00:00+08:00
Not the result what I need. And how to? Thanks.
How about this?
echo date("c",'1338048000');
I'd say:
gmdate('Y-m-d\TH:i:s\Z', '1338048000');
The T means something, and needs to be escaped. Or, since PHP5, the ISO8601 date format is natively supported with the c character.
Additionally, using gmdate instead of date removes the need to worry about timezones.
echo date("c", "1338048000").'+08:00';

Categories