Date gives as result 2024 instead of 1924 Twig - php

In the database, I have following date: 1924-01-17, as a date-type, but when I render it as {{ person.BirthDate | date("d/m/Y") }}, it gives as a result 17/01/2024.
How can I solve the Year-problem?

Twig documentation for date says:
The date filter accepts strings (it must be in a format supported by
the strtotime function), DateTime instances, or DateInterval
instances.
DateTime instance should do the trick, and based on comments it fixed this.

Related

How to change DateTime format to a desired format via Carbon laravel

I'm using laravel 5.5 and Carbon library to write an API.
I store all date and time values in MySQL database as common YYYY-MM-DD HH:MM:SS format.
But on the other hand front-end developer ask me to return all dates as 2017-12-20T20:30:00.000Z. seems that is a common format in JavaScript.
Is there any way to convert all DateTime formatted fields to desired format via laravel or Carbon ?
Update:
First problem is solved but another one is that client want to send date times as same 2017-12-20T20:30:00.000Z for all fields of table. while I should get and save them as common DateTime. What can I do in this case?
Javascript uses ISO 8601 syntax. In PHP, you can use like this:
date('c', strtotime($yourDateTime)); // c is ISO 8601 format
Reference here:
Since the date is casting as Carbon, use format():
$date->format($desiredFormat);
The list of characters for creating $desiredFormat will also be helpful.
Laravel Provide getter and setter method for format Eloquent attribute values
by default laravel return 'dateTime' as carbon instance
so, you can change format in model using getter method
public function getDateTimeAttribute($value)
{
$desiredFormat = "Y-M-d"; //change format as per your requirement
$value->format($desiredFormat);
}
Link for more about getter and setter method
https://laravel.com/docs/5.5/eloquent-mutators
{{ date('M j, Y h:ia', strtotime($created_at)) }}
It gives us like "Dec 30, 2017 16:39am"
You can edit you date time of course(M j, Y h:ia) just google it "php: date - manual"

Trying to convert a date in Twig

I'm trying to convert this:
{{ "26/03/2013"|date("d/m/Y") }}
in Twig but its is throwing the error
Uncaught Exception: DateTime::__construct(): Failed to parse time
string (26/03/2013) at position 0 (2): Unexpected character in
/home/vagrant/Code/Phantom
Website/vendor/twig/twig/lib/Twig/Template.php on line 218.
If I pass this:
{{ "03/26/2013"|date("m/d/Y") }}
It works, so I imagine I need to change something related to Twigs date formatting
The date filter is about formatting DateTime Object, so if you pass a string this will be passed to the constructor of the DateTime object then to the format method, so in your case, you need to format the string that looks good for a DateTime constructor as example
{{ "2013-3-26"|date("d/m/Y") }}
From the doc:
The format specifier is the same as supported by date, except when the
filtered data is of type DateInterval, when the format must conform to
DateInterval::format instead.
And also about string format:
The date filter accepts strings (it must be in a format supported by
the strtotime function), DateTime instances, or DateInterval
instances. For instance, to display the current date, filter the word
"now":
Try this in this twigfiddle
If you use /'s as delimiter the expected format is m/d/Y,
To pass the date as day, month, year you need to use a - as delimiter
{{ "26-03-2017" | date('d/m/Y') }}
fiddle

Twig, format date SQL Server

I using Twig in a PHP application,
The PHP object that I use has an attribute called "date", got from SQL Server.
It's look like : "Mar 2 2014 12:00:00:000AM"
I try to convert it using Twig for display it, I try with | date("Y-m-d") without success :
An exception has been thrown during the rendering of a template ("DateTime::__construct() [function.DateTime---construct]: Failed to parse time string (Jun 20 2013 12:00:00:000AM) at position 20 (:): Unexpected character") in "..." at line 96.
Any ideas ?
Thanks,
Have a nice day.
The date filter can work on \DateTime instances and strings that can be passed to strtotime(), apparently yours isn't.
You mention you're using an object that has the date property, I recommend adding a new function to it:
public function getDateAsObject()
{
// should be able to parse this format: Mar 2 2014 12:00:00:000AM
return \DateTime::createFromFormat("M j Y h:i:s:uA", $this->date);
}
You may need to adapt the format and the functions name for your conventions.
You can use it in your template:
{{ your_object.dateAsObject|date("Y-m-d" }}
You don't need to use date('Y-m-d'), base on the error message your object's date attribute is a type of DateTime, so use following:
// I assume 'object' is your object which has 'date' as the attribute
<span>Date: </span>{{ object->date->format('Y-m-d') }}
Check documentation for DateTime::Format

Django equivalent of PHP's date

Does anybody know something similar to function date() from PHP in Django?
I'm starting to learn this nice framework, I'm very happy with it (at the momment) but i haven't found that function
Yes.
date
Formats a date according to the given format.
Uses a similar format as PHP’s date() function (http://php.net/date) with some differences.
For example, you can do:
{{ value|date:"D d M Y" }}
If value is a datetime object (e.g., the result of datetime.datetime.now()), the output will be the string 'Jul 22 2013'.
Documentation:
https://docs.djangoproject.com/en/dev/ref/templates/builtins/#date
https://docs.djangoproject.com/en/dev/ref/settings/#date-format
https://docs.djangoproject.com/en/dev/topics/i18n/timezones/#naive-and-aware-datetime-objects

how do i use timezone in Twig date filter?

I am using Twig and this date filter
http://www.twig-project.org/doc/templates.html#date
Apparently they are looking out for DateTime instances in the parameter.
looking at this http://www.php.net/manual/en/datetime.construct.php
I have trouble understanding the php datetime object and how to use the timezone.
Given that i know basic PHP and is familiar with simple web programming, how do I use it to display a date and time using the Twig date filter while catering for timezone?
If there is a simpler way to do it while using the date filter, but NOT using datetime object, i would be open to it.
I am only concerned that the solution works, rather than the "correctness" or "elegance" of the solution.
The "Date" filter of Twig accept a second parameter: "timezone".
So, you can easily display all timezone that you want. For example:
{{ "now"|date("m/d/Y H:i", "Europe/Paris") }}
{{ "now"|date("m/d/Y H:i", "Asia/Calcutta") }}
{{ "now"|date("m/d/Y H:i", "Europe/Berlin") }}
For more informations:
http://twig.sensiolabs.org/doc/filters/date.html#timezone
In today's version, it has been supported in symfony application config file:
twig:
date:
timezone: Asia/Tokyo
http://symfony.com/blog/new-in-symfony-2-7-default-date-and-number-format-configuration
i know the question is old, but this is for reference.
By default Twig is going to use the default timezone that is set in php ini file or in the application globally, or the declared in twig.
if you pass a datetime object to the date filter with timezone , then you can pass false as a second argument in date filter.
{{ dateForSomething | date('theFormatIWant', false) }}
please refer to documentation
twig date
I think you might have misread the documentation.
The date filter accepts any date format supported by DateTime and DateTime instances.
That means that you can just pass in things like "2011-01-20 12:00:00" OR a real DateTime Object.
But you don't have to deal with the object if you don't want do.
Now if you need that string to be displayed in a specifiy timezone I would set that timezone in php before passing it to twig
$x = new DateTime("2010-01-01 12:00:00");
$x->setTimezone(new DateTimeZone("The Timezone you need"));
// pass to twig
What worked for me is adding a new filter, so it always looks at the timezone within the DateTime obj.
Below the example with the DateTime object as a parameter.
I may be missing something by I don't understand why Twig is ignoring the DateTime Timezone part and using the default global one.
In the filters extension:
public function getFilters()
{
return array(
(...)
new \Twig_SimpleFilter('date_tz', array($this, 'dateTzFilter')),
);
}
and
public function dateTzFilter(\DateTime $dateTime)
{
return $datTime->format('desired_format');
}

Categories