Format DateTime with PHP - php

My API outputs the DateTime in the following format:
2018-06-17T09:07:00Z
How do I display this in a more meaningful way, say, 17/06/2018.
I looked at the Manual: http://php.net/manual/en/datetime.formats.date.php however still wasn't able to find a way to achieve this.
$eventStart = "2018-06-17T09:07:00Z";

You can format it like the below code in PHP:
echo date('d/m/Y', strtotime('2018-06-17T09:07:00Z'));

Use Below code.
date('d/m/Y', strtotime('2018-06-17T09:07:00Z'));

Use Date Format :
$inputDate = "2018-06-17T09:07:00Z";
echo date('d/m/Y', strtotime($inputDate));

Convert the string in time and then format the date which you want
Date-Format option
$date = '2018-06-17T09:07:00Z';
echo date('d/m/Y', strtotime($date));

Format the date, but first convert the string to time.
echo date('d/m/Y', strtotime($inputDate));

Related

Changing date format from MySQL in php

Currently, this code : <?php echo $user->last_login?> is showing this date format YYYY-MM-DD from MySQL. How do i change it to this format DD-MM-YYYY?
Thanks.
Edit : Problem solved. $date = new DateTime($user->last_login);
echo $date->format('Y-m-d H:i:s');
Thanks guys!
You can do this:
date(d-m-Y", strtotime($user->last_login));
Or this:
$date = new DateTime($user->last_login);
echo $date->format('d-m-Y');
Or use Carbon (http://carbon.nesbot.com/docs/):
Carbon::createFromFormat('Y-m-d', $user->last_login)->format('d-m-Y');
Actually #frayne-konok was close. You'll need to do
echo date('d-m-Y', strtotime($user->last_login));
See the PHP docs for more date formatting codes: http://php.net/manual/en/function.date.php

strtotime() doesn't recognize german shortform of date (dd.mm.YY)

I have much data with several timestamps and I just recognized that some are in "dd.mm.YYYY" which works very well with date("Y-m-d", strtotime($input)); but some are in "dd.mm.YY" and this does not work anymore - it always returns the current date.
My problem is that my data is too huge to fix this problem manually by editting. Is there any way to get the YYYY-mm-dd out of mm.dd.YY ?
Here you go...
$date = "20.02.71"; // sample date... (common German format)
$date = DateTime::createFromFormat('d.m.y', $date);
echo $date->format('Y-m-d');
will result in:
1971-02-20
Create a DateTime object, then format it to anything you want...
Well you can replace the . by -, you could do something like the following:
$date = str_replace(".", "-", "mm.dd.YY")
This would return
mm-dd-YY
You could use date_parse_from_format which would convert any formate into the formate you specify.
date_parse_from_format("y-m-d", $date);
It returns an array with very useful information like month, year etc.

Date function in PHP Not working properly

While converting date format from mm-dd-yy hh:ii:ss to yy-mm-dd hh:ii:ss format using below code.
<?php
echo $start_date = date("Y-m-d H:i:s",strtotime("10-14-2015 00:00:00"));
?>
But the result is
1970-01-01 05:30:00
If it is not a proper way to use date ,provide me alternate way
First check this answer whats the difference between dates over here and simply use str_replace like as
echo $start_date = date("Y-m-d H:i:s",strtotime(str_replace("-","/","10-14-2015 00:00:00")));
Or you can also use DateTime::createFromFormat over like as
$date = DateTime::createFromFormat("m-d-Y H:i:s","10-14-2015 00:00:00");
echo $date->format('Y-m-d');
Demo
Read the manual: http://php.net/manual/en/function.strtotime.php
and also specify your PHP version.
It has a nice example on checking if strtotime conversion was successfull:
if (($timestamp = strtotime($str)) === false) {
And this conversion is very critical, it supports only limited number of formats that should be specified very precisely in order days/months/years, and separators / or - or :
So you have to pick the format that you will support from following list:
http://php.net/manual/en/class.datetime.php
Be sure to create culturally aware code (e.g. US/UK/... format)
Please write your code as below:
echo $start_date = date("Y-m-d H:i:s",strtotime("10/14/2015 00:00:00"));

convert dd-mm-yyyy to yyyy-mm-dd in php

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');

How to get current date in codeigniter

How to get current date in codeigniter in YY-mm-dd format. I wants to get current date in YY-mm-dd frmat and put this value into input text box
You can use the PHP date function.
date('Y-m-d');
Up to my knowledge, there is no separate date function in codeigniter.
EDIT :
But if you want date in this format 13-04-05 [ yy-mm-dd ], Try this
date('y-m-d');
For more date formats, check this link PHP Date Formats
Try to use this is a generic format of DateTime
echo date('Y-m-d H:i:s');
use php date function
echo date("Y-m-d");
will give you the result
What about:
$date = new \Datetime('now');
var_dump($date);
if you want the full date:
echo date('Y-m-d');
depends your date structure.
echo date('d-m-Y');
if you want year only, then echo date('Y'); is enough
Use php date() function, like this
echo Date('Y/m/d');
it give you the desired result!

Categories