how to convert symfony2 datetime object from MongoDB - mongodb

Hi I need to check multiple dates for reminder section of my application.
But when I fetch dates from mongodb I am getting this:
DateTime Object
(
[date] => 2014-03-31 15:00:00
[timezone_type] => 3
[timezone] => Europe/Berlin
)
I am trying this to access date parameter but it is showing me error that dateTime object can not converted into string.
I am looping dateandtime.
Please let me know how can I access date in this situation in symfony.
Thanks

The assumption of others is that you recieve a MongoDate object back but you don't since I beleive Symfony actually converts it to a DateTime: http://www.php.net/manual/en/class.datetime.php (I think this comes from Doctrine 2)
What you can do is use the format function: http://www.php.net/manual/en/datetime.format.php like so:
$date->format('Y-M-d h:i:s')

You can do this in php (symphony is a php-framework).
All you need is something like this:
date('Y-M-d h:i:s', $yourDateTimeObject->sec);
You can also use __toString method from MongoDate class, but I actually prefer the previous method with sec.

Related

CakePHP 3 Date format Issue

I researched everywhere but could not find the solution,
In the database, I am using from_date field as date(2018-07-07) type
But when I find the data then it auto converts in from_date to 'Cake\I18n\Date Object'
[from_date] => Cake\I18n\Date Object
(
[time] => 2018-07-07T00:00:00+00:00
[timezone] => UTC
[fixedNowTime] =>
)
I what this as
[from_date] => '2018-07-07'
I know we can covert this by
$obj->from_date->format('Y-M-D');
but the data is coming in an array of 10000 loops, this is not a good idea to convert in the loop.
I also tried this in bootstrap.php
Type::build('date')->useLocaleParser()->setLocaleFormat('y-m-d');
but this is also not working.
Please help
in your bootstrap.php you can set the default format you want when converting Date and FrozenDate to string in Json
see the manual here and the api here
note that you have to use string patterns described here and not the patterns you usually use with date()
so your code should be
\Cake\I18n\FrozenDate::setJsonEncodeFormat('yyyy-MM-dd');
\Cake\I18n\Date::setJsonEncodeFormat('yyyy-MM-dd');

Laravel 5, PgSQL and Date format

I'm struggling with Laravel date formats, using it with PgSQL.
My need :
input date must be in this ISO8601 format: Y-m-d\TH:i:s\Z (example: 2016-05-13T10:05:00Z)
output must be in the same format
PgSQL stores it in the format Y-m-d H:i:s (example: 2016-05-13 10:05:00)
The hard part is to configure Laravel to globally accept a particular date format in input and output, but without changing "internal" format.
A property can be overridden to customize format, but it acts globally:
protected $dateFormat = 'Y-m-d\TH:i:s\Z';
This leads to errors when retrieving dates from database (trying to convert a PgSQL date with a custom mask: InvalidArgumentException).
I would like to avoid using a Middleware or do it manually in each controller response, is there any way?
Sounds like you want to use accessors and mutators. Here's a basic example with a class that has a my_date column, the conversion might not be exactly right (I'm just assuming strtotime() will work) but that should be easy for you to fix.
class SomeModel extends Model
{
// triggered whenever you retrieve the date from the db
public function getMyDateAttribute($value)
{
return date('Y-m-d\TH:i:s\Z', strtotime($value));
}
// triggered whenever you insert the date into the db
public function setMyDateAttribute($value)
{
$this->attributes['my_date'] = date('Y-m-d H:i:s', strtotime($value));
}
}
Now, your database can use one datetime format and the other parts of your application can use another. If you want it to be reusable for other models, you can extract it to a trait as long as the columns are named the same.

Laravel Date calculation

I need to set end_at attribute 30 days from current date. how can i do that in laravel 4.
When I have used bellow code I am getting error saying "Class 'Date' not found"
Please help me to fix this.
$sub->end_at = new Date('+30 days');
There is no Date class in PHP, there is only a DateTime class which you could use.
But since you're using Laravel, which uses the Carbon library by default, you can use that to handle dates because it has a better API. In your case you can do this:
use Carbon\Carbon;
...
$sub->end_at = Carbon::now()->addDays(30)->toIso8601String();
If you're trying to update a Eloquent model, then you can take advantage of Eloquent's integrated date/time column handling. In your model you can add the dates property with this value:
protected $dates = ['end_at'];
and now when assigning a timestamp to the end_at column, Laravel will automatically transform and save it to the correct format in your database. So you'll only need to use this:
$sub->end_at = Carbon::now()->addDays(30);
This will return that in the format appropriate for MySQL
$sub->end_at = date('Y-m-d H:i:s', strtotime('+30 days'));

ruby on rails scaffold form with :date field, converting it to string

I have a scaffold form with a field 'date:date' and I want to convert it to a string after the form has been submitted to use it in creating another model, what is the best way of doing this? This has some info but none of the methods work, maybe the scaffold date object is different from a regular Date object?. I know I can rescaffold it as a string but I need to convert it.
review_params[:date].to_s(:db) returns a NoMethodError
This worked for me, if anyone else has same issue with extracting date from 1 form and using it to create another model.
date_string = "#{review_params['date(1i)']}-#{review_params['date(2i)']}-#{review_params['date(3i)']}"
#concert = Concert.find_or_create_by!(artist: review_params[:artist], venue: review_params[:venue], date: date_string)

Grails date type

In my view I have something like this:
Date: ${it.date}
which has the following output:
Date: 2011-05-24 00:00:00.0.
How can I change the Date formation so that the last part(00:00:00.0) does not appear?
Yes, you can format a date with the related tag, which is , like this:
<g:formatDate format="yyyy-MM-dd" date="${it.date}"/>
Here more info: http://www.grails.org/GSP+Tag+-+formatDate
Hope this helps. :)
If you're doing a lot of date manipulations, I'd highly suggest getting the JodaTime plugin http://www.grails.org/plugin/joda-time. It has its own tag library, and formatting the DateTime can be simply done using the toString() method.
DateTime dt = new DateTime()
println (dt.toString("YYYY-MM-dd"))
In your case you could do the following (if using JodaTime):
Date: ${it.toString("yyyy-MM-dd")}