I want to save Y-m-d date format to field date
#[ORM\Column]
private ?\DateTimeImmutable $created_at = null;
When I save this formatted date:
$date = new DateTimeImmutable();
$date->setTimestamp($timestamp);
$date->format("Y-m-d");
Symfony saves with the following format:
2023-01-17 14:34:55
When I try to save a date like this:
$date = date("Y-m-d", $timestamp);
I get an error:
must be of type DateTimeImmutable, string given
What am I missing?
There are two problems that play a role here:
1: your entity expects an DateTimeImmutable Object, but you provide a string. Thus to solve this, use The DateTimeImmutable object to create and pass the correct object type your method asks for.
2: You want to save your date in a specific format, Y-m-d. To do that, change your ORM\Column type to date instead of dateTime
If you want to use Y-m-d format in your project, use the correct formatting to achieve that.
Take a look at the docs for deeper understanding.
To save the date in Y-m-d Format, you can use createFromFormat method
$date = DateTimeImmutable::createFromFormat("Y-m-d", date("Y-m-d", $timestamp));
I hope it will solve your problem. Let me know if you have any question
Related
In my controller, when I create an event, it saves perfectly fine. The user enters a date in dd-mm-yyyy and it gets saved in MySQL DATETIME format. Then the details view renders it completely fine, just like the edit view via model binding.
As soon as I try to save from the edit form, the date somehow fails and returns 1970-01-01 00:00:00.
I am not really sure why this only happens on my update method, as my store method is in essence the same.
$input = Input::all();
$input['plannedTime'] = date('Y-m-d H:i:s', strtotime(Input::get('plannedTime')));
How comes the update method returns no error, validation is alright, but it still won't save it correctly?
The value of 'plannedTime' is a string of date format d-m-Y H:i:s.
There's your problem. PHP's strtotime does its best, but 04-05-2015 00:00:00 could be either April 5 or May 4, depending where you live.
As an example, on my install, strtotime('04-13-2014 00:00:00') fails (which'll get converted to 0, which'll get converted to 1970-01-01).
Laravel's date validation expects a value that strtotime can handle. If you're using an ambiguous date format, use createFromFormat to parse it, then format to spit it out in a more standard format.
$date = DateTime::createFromFormat('d-m-Y H:i:s', Input::get('plannedTime'));
$usableDate = $date->format('Y-m-d H:i:s');
MySQL never returns any error for invalid dates as far as i can remember, it just sets it to EPOCH and thats it!
You should enforce your dates to be always converted to a "Y-m-d H:i:s" format when communicating with your site and display the date in "d-m-Y" only on the output side.
You should try and use
public static DateTime DateTime::createFromFormat ( string $format , string $time [, DateTimeZone $timezone ] )
To create a datetime object from a format. If you know your format such as in this case, then provide the format and get a date object back which will be persisted correctly!
This should be simple, but I'm having trouble... so turn to StackOverflow...
I'm in the UK and am getting a date from the jQuery DatePicker in a dd/mm/yy format.
I want to store this date as a serial (yyyymmdd) so run a Date_To_Serial function that just does:
return date("Ymd", strtotime($strDate_In));
where $strDate_In is the date string in dd/mm/yy format.
So, passing in 01/12/2013, I expect 20131201 to be returned.
But, when 01/12/2013 is passed in, it returns 20130112 - PHP obviously assumes the date format is mm/dd/yyyy.
How can I fix this please?
If you know the format, try using the static createFromFormat method on the DateTime class:
$date = DateTime::createFromFormat('d/m/Y', '01/12/2013');
return $date->format('Ymd');
If the separator is a / then strtotime assumes a US-format. To have it recognise a UK format the separator must be - or .:
echo date('Ymd', strtotime('01/12/2014')); // 20140112
echo date('Ymd', strtotime('01-12-2014')); // 20141201
So for this to work in your example you would do:
return date("Ymd", strtotime(str_replace('/', '-', $strDate_In)));
Use a DateTime object's createFromFormat method. It allows you to specify the format of the input.
Afterwards, use the format method to export the date in the desired format.
Check out DateTime::createFromFormat for correct handling of non-standard date formats.
return DateTime::createFromFormat("d/m/Y", $strDate_In)->format("Ymd");
I have this code:
$dateTime = new DateTime('#'.strtotime('+30 minutes'));
$dateTime->setTimezone(new DateTimeZone($someModel->timezone));
$otherModel->send_at = $dateTime->format($otherModel->getDateTimeFormat());
Where $otherModel->getDateTimeFormat() returns M/d/yy h:mm a which is fine because it is based on the current Yii locale which is based on CLDR as far as i know.
Now, when i pass this format to PHP's DateTime::format() class method [$dateTime->format($otherModel->getDateTimeFormat())] i get this result: Dec/06/1313 03:1212 pm which is looking weird because the format that php accepts for date/datetime is not the same as the one Yii is using in it's locales.
How should one fix such issue?
This is the fix:
$dateTime = new DateTime('#'.strtotime('+30 minutes'));
$dateTime->setTimezone(new DateTimeZone($someModel->timezone));
// get a timestamp from the current date that also knows about the offset.
$timestamp = CDateTimeParser::parse($dateTime->format('Y-m-d H:i:s'), 'yyyy-MM-dd HH:mm:ss');
// now format using Yii's methods and format type
$otherModel->send_at = Yii::app()->dateFormatter->formatDateTime($timestamp, 'short', 'short');
The idea is to use the PHP's DateTime::format() method to extract the timestamp that has taken into consideration the user timezone. Then, based on this timestamp, format according to Yii datetime formatting.
Well, nothing strange, your date format is M/d/yy h:mm a, and according to DateTime::format() documentation :
M : A short textual representation of a month, three letters
y : A two digit representation of a year
...etc
Yii does not use the same format :
http://www.unicode.org/reports/tr35/tr35-dates.html#Date_Format_Patterns
You should simply use CDateFormatter::format() : http://www.yiiframework.com/doc/api/1.1/CDateFormatter#format-detail
I have date
$timeZome = timezone_open('Europe/Kiev');
$date = new DateTime();
$date->setTimezone($timeZome);
$date->setDate(2011, 06,25);
$date->setTime(11,35,00);
How to present like that?
20110625T040000Z
This will do what you want:
$date = new DateTime();
$date->setTimezone(new DateTimeZone('Europe/Kiev'));
$date->setDate(2011, 06,25);
$date->setTime(11,35,00);
$date->setTimezone(new DateTimeZone('UTC'));
echo $date->format('Ymd\THis\Z'); // format string to match question
It's pretty straightforward: set the date/time/zone, modify the timezone and format for printing.
You may just need to call format on your date object, I think.
echo $date->format('Ymd'); // 20110625
UPDATE:
You may want to look into either using the existing constants in the date class, or if your needs differ from what date offers, simply extend it and add a method that parses and returns your date formatted as you want it to.
I want to input a timestamp in below format to the database.
yyyy-mm-dd hh:mm:ss
How can I get in above format?
When I use
$date = new Zend_Date();
it returns month dd, yyyy hh:mm:ss PM
I also use a JavaScript calender to insert a selected date and it returns in dd-mm-yyyy format
Now, I want to convert these both format into yyyy-mm-dd hh:mm:ss so can be inserted in database. Because date format not matching the database field format the date is not inserted and only filled with *00-00-00 00:00:00*
Thanks for answer
Not sure if this will help you, but try using:
// to show both date and time,
$date->get('YYYY-MM-dd HH:mm:ss');
// or, to show date only
$date->get('YYYY-MM-dd')
Technically, #stefgosselin gave the correct answer for Zend_Date, but Zend_Date is completely overkill for just getting the current time in a common format. Zend_Date is incredibly slow and cumbersome to use compared to PHP's native date related extensions. If you don't need translation or localisation in your Zend_Date output (and you apparently dont), stay away from it.
Use PHP's native date function for that, e.g.
echo date('Y-m-d H:i:s');
or DateTime procedural API
echo date_format(date_create(), 'Y-m-d H:i:s');
or DateTime Object API
$dateTime = new DateTime;
echo $dateTime->format('Y-m-d H:i:s');
Don't do the common mistake of using each and every component Zend Frameworks offers just because it offers it. There is absolutely no need to do that and in fact, if you can use a native PHP extension to achieve the same result with less or comparable effort, you are better off with the native solution.
Also, if you are going to save a date in your database, did you use any of the DateTime related columns in your database? Assuming you are using MySql, you could use a Timestamp column or an ISO8601 Date column.
This is how i did it:
abstract class App_Model_ModelAbstract extends Zend_Db_Table_Abstract
{
const DATE_FORMAT = 'yyyy-MM-dd';
public static function formatDate($date, $format = App_Model_ModelAbstract::DATE_FORMAT)
{
if (!$date instanceof Zend_Date && Zend_Date::isDate($date)) {
$date = new Zend_Date($date);
}
if ($date instanceof Zend_Date) {
return $date->get($format);
}
return $date;
}
}
this way you don't need to be concerned with whether or not its actually an instance of zend date, you can pass in a string or anything else that is a date.
a simple way to use Zend Date is to make specific function in its business objects that allows to parameter this function the date format. You can find a good example to this address http://www.pylejeune.fr/framework/utiliser-les-date-avec-zend_date/
this is i did it :
Zend_Date::now->toString('dd-MM-yyyy HH:mm:ss')
output from this format is "24-03-2012 13:02:01"
and you can modified your date format
I've always use $date->__toString('YYYY-MM-dd HH-mm-ss'); method in the past but today didn't work. I was getting the default output of 'Nov 1, 2013 12:19:23 PM'
So today I used $date->get('YYYY-MM-dd HH-mm-ss'); as mentioned above. Seems to have solved my problem.
You can find more information on this on output formats here: http://framework.zend.com/manual/1.12/en/zend.date.constants.html