convert dd-mm-yyyy to yyyy-mm-dd in php - 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');

Related

This string date "10/05/2025" is not converting to dateformat php

This is my code below :
$startDate= str_replace('/', '-', $bhishi_date);
$startDate=strtotime($startDate);
$startDate= date('Y-m-d', strtotime($startDate))
Try Below Code its help for you.
$startDate= date('Y-m-d', strtotime($bhishi_date))
You can use Carbon too:
$startDate = \Carbon\Carbon::parse('10/05/2025');
To deal with dates and times, I prefer to use the build-in DateTime class with which you can do everything in a consistent syntax.
$startDate = \DateTime::createFromFormat('d/m/Y', '10/05/2025')->format('Y-m-d');
// input format ---------^^^^^ output format ---------^^^^^

get date from dateTime in PHP

I want to extract date without time from a single value from array result thaht I got.
I tried using array_slice but it didn't work.
My code..
$dateRows = $this->get('app')->getDates();
$dateRow = json_decode(json_encode($dateRows[0], true));
dump($dateRow[0]);die;
And I got result..
"2014-01-01 00:00:00"
And I want it to return just
"2014-01-01"
Very Simple, just use date_create() on your date & then format it using date_format() as follows -
$date = date_create("2014-01-01 00:00:00");
echo date_format($date,"Y-m-d");
So, in your case, it would be something like -
$date1 = date_create($dateRows[0]);
echo date_format($date1,"Y-m-d");
you can use strtotime function as well for getting formatted data
$a = "2014-01-01 00:00:00";
echo date('Y-m-d', strtotime($a));

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

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

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!

Categories