Changing date format from MySQL in php - 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

Related

Format DateTime with 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));

Get tomorrows date from the webpage

What is a php function that i can make call tomorrows date formatted like this? 02/04/2014
So if im looking at the website on 2/3/2014 it will show 02/04/2014
If i look at it on 2/15/2014 it will show 2/16/2014
Just add using strtotime() / date() functions:
echo date('m/d/Y', strtotime('+1 day'));
Update
You can also do it using PHP's DateTime and DateInterval classes:
$date = new DateTime();
$date->add('P1D');
echo $date->format('m/d/Y');
This should work ..
<?php
function tomorrow()
{$date = date('m/d/Y');
sleep(24*60*60);
return $date;}
echo tomorrow();
?>

PHP date dd-mm-yyyy

I would like to fill a date in as dd-mm-yyyy but I would like to save the date as Y-m-d in my database. What I have is this:
Form:
$factuurDatum = new Zend_Form_Element_Text('datum');
Controller:
$data = $addInkoopfactuur->getValues();
$data['datum'] = date("Y-m-d", strtotime($data['datum']));
But it doesn't work. I still get an error: '08-01-2014' and does not fit the date format 'yyyy-MM-dd'. How can I resolve this problem?
Use this DateTime class
<?php
$pubDt='07-01-2014';
$date = DateTime::createFromFormat('d-m-Y', $pubDt);
echo $newPubdate = $date->format('Y-m-d');
DEMO : https://eval.in/86800
Did you want it?
$data['datum'] = "15-10-2013";
$data['datum'] = date("Y-m-d", strtotime($data['datum']));
echo $data['datum'] ;
OUTPUT:
2013-10-15
Got the problem myself! the addvalidator('Date', true) should be deleted in my form! Because the validator will set my date as yyyy-mm-dd. My question was not answered clear enough, sorry!

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