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!
Related
I have a variable is which the value coming is Date along with time in php. How do I convert it into a variable to get only the year? I do not need automatic updation but the format change is needed. Normal answers are giving it about date but my variable is containing time as well.
The format coming by now is 2017-12-11 4:06:37 and i need only 2017
Use like this:
<?php echo date('Y',strtotime('now'));?>
You can you simple DateTime function and date_formate() function for displaying separate year, month and date.
For that you have to first convert in Object of your current Date time string by using :
$date = new \DateTime('2017-12-11 4:06:37');
And then you can use date format function by using below code:
echo date_format($date, "Y"); //for Display Year
echo date_format($date, "m"); //for Display Month
echo date_format($date, "d"); //for Display Date
You can code like this (working perfectly):
$format = 'Y-m-d H:i:s';
$date = DateTime::createFromFormat($format, '2009-02-15 15:16:17');
echo "Format: $format; " . $date->format('Y') . "\n";
As mentioned by Himanshu Upadhyay, this is correct and the easiest way.
<?php
echo date('Y',strtotime('now'));
?>
But i would recommend you to read this here. You should really do actually!
By using DateTime class
$date = new \DateTime('2017-12-11 4:06:37');
echo $date->format('Y');
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
I am using this function to change date format.
echo date("mdy", strtotime('2013-11-04'));
It's return: 110413
Now i want to decode it.
How to get date like this format: 2013-11-04
Remember 110413 should be as a input of that function
Thanks
Try like this
<?php
$MyDate="110413";
$date = DateTime::createFromFormat('mdy',$MyDate);
echo $newformat=$date->format('Y-m-d');
?>
$mydate=date("Y-m-d", strtotime('2013-11-04'));
echo date("Y-m-d", strtotime($mydate));
try like this,
$date = date("mdy", strtotime("2013-11-04"));
$myDateTime = DateTime::createFromFormat('mdy', $date);
$newDateString = $myDateTime->format('Y-m-d');
echo $newDateString;
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 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!