Eloquent Compare time - laravel

I have a Model which stores the total time spent on page in, the format is HH:MM:SS, i am trying to ask the user to input time in HH:MM:SS and then I would compare the entries value to the one stored in the database.
here is my query so far but its not yeilding any results back.
$assignees = EntryKPITable::where("total_time_inside",'>',$request->number)->whereBetween('created_at',[$from, $to])->get();
I have searched through Eloquent module and only found "WhereDate()" which compare time+date.
all the questions i found did a comparison using DATETIME format(total time can be more than 24h in my case)

Related

Using time field in Eloquent model

I have got a database field containing only a time, it's declared in my migration as
$table->time('time')->default('00:00:00');
Now I want to add a number of seconds to this field using Eloquent, doing something like:
$activetime->time->add(new DateInterval('PT5S'));
(where $activetime is a object of a Eloquent model)
First try on this I got
Call to a member function add() on string
So obviously I have to tell Eloquent that this is not a string but a time. The closest I have got is putting it in protected $dates in the model, but then instead I get
Unexpected data found.
I suspect this is because it isn't really a date but only a time, but I can't seem to find much information about how to handle times. Is there any way of solving this or would I be better off using something else, like dateTime or timestamp (or even an integer for amount of seconds)? What I DO want to record is a users amount of active time for a given date, so for that purpose time seems like the natural choice...
This is because Laravel reads time database type as just a string and you are expecting a DateTime Object.
Add this to your Eloquent model:
public function getTimeAttribute($value)
{
return new \DateTime($value);
}
You can also cast your time to DateTime Object using Laravel Attribute Casting, but you'll have to append some date to your time, because Laravel is expecting correct format

Laravel: How to deal with dates in different timezones

A lot of questions have been asked about this subject. The best answer that I found is this one: How to set local timezone in laravel
So the main rule is to keep all database entries in the same timezone.
But I have a specific case where this answer does not work for me. For some models, I have only a date (no datestamp). Example: suppose that I only store the date of when this question was asked (= 2018-01-25). However in Europe it is already 2018-01-26. Someone has a solution for this?
Changing my date field to a datestamp? What with existing dates?
You can use this library jamesmills/laravel-timezone
OR
If you need custom configuration:
Configure your app timezone to UTC.
'timezone' => 'UTC',
You can store different timezones in database column.
When outputting/displaying dates, just format it to use that timezone.
$timezone = 'America/Vancouver';
$model->created_at->setTimezone($timezone);
created_at and updated_at are automatically converted to carbon instances, which makes this easier. If you have other dates that you're using, add them to the protected $dates array on the model and laravel will convert them to carbon instance too. Then you can use carbons setTimezone() to change the date/time to the timezone.
If you're only talking about a date, then there is no time component and thus time zones are irrelevant. For this reason, most platforms do not have a separate date-with-zone type.
You're correct that not every time zone experiences the same date at all times, and that the start of a date and the end of the date occur at different times in different time zones. However, did you notice that in the prior sentence that I had to use the word "time" to rationalize about these points? :-)
Because date and time zone don't come together without time, there's no purpose in keeping them in the same field. Instead, keep two fields in your model - one for the date, and one for the time zone. In many cases, you may even find they belong in two different models.
As a use case example, consider birthdays. Mine is 1976-08-27. That's all it is - just a date. The time zone of my birth is irrelevant, and so is the time zone I'm located in - until I want to evaluate whether it's currently my birthday (or how long until my birthday, etc.) For those operations, my current time zone is important, and so is the start time-of-day and end time-of-day of that time zone. Thus - two different fields.

MSCRM OlderThanXHours and the current time

I created a QueryExpression with one of the Conditions set to ("dev_lastremarketingtime", ConditionOperator.OlderThanXHours, 1); 
The intent is the retrieve multiple records that are an hour after the value of dev_lastremarketingtime.
There are records in the table that meet this criterion, however, they are not returned. When I examine the record (RetrieveMultiple of a specific record this time) it seems that the times used for comparison are not consistent. 
For example, when viewing the account record in question, the dev_lastremarketingtime's value as shown on the account form is 5/17/2017 8:57 PM. the internal is time 5/18/2017 12:57:27 AM.  The wall clock at the time of the test shows 5/17/2017 10:38:11 PM (or 5/18/2017 2:38:11 AM UTC). This record should have been returned by the query (the current time is more than one hour after the value of dev_lastremarketingtime)
If I wait a few hours and run my app again, the expected records are retrieved.
It seems, therefore, that the Query expression with the ConditionOperator.OlderThanXHours is comparing the current Eastern Daylight time with the record's UTC time. 
How can I get this query to compare the same time zones (I don't think it matters which one)?
(My Personal options include the setting for Eastern Time
Instead of using the OlderThanXHours operator, maybe try using "less than", which provides control of the DateTime value to filter on.
This way you can use the UTC time minus an hour, like this:
var qe = new QueryExpression("myentity");
qe.Criteria.AddCondition("dev_lastremarketingtime", ConditionOperator.LessThan, DateTime.UtcNow.AddHours(-1));

Neo4j: how to query by intermediate date when given date range

Neo4J TimeTree is an efficient way of modelling time in a graph. However, I'm interested in how best to model/query for an object with a defined start and end time.
For instance, a ticket might be validFrom and validTo given dates, which may be separated by many days. A user may have many tickets.
For a given date, what is the most efficient way of querying for valid tickets?
When entering the data, I suppose I could create lots of validOn relationships between a ticket and the intermediate days between the start and end, but this seems inefficient. Can anyone think of a better way of querying the data?
I can start from a user and find all tickets for that user whose validFrom is <= and validTo is >= the date. However, what happens if I need to start from a date? I.e. match all tickets that are valid on a given date?
You only link the ticket to the validFrom and validTo dates with dedicated relationships.
For any given day, you query backwards for tickets that have their :START relationship before that date but the :END relationship after that date, something like this:
MATCH path = (t:Ticket)-[:START]->(before:Day)-[:NEXT*0..30]->(day:Day {date:{date}})
WHERE (t)-[:END]->(:Day)<-[:NEXT*1..30]-(day)
RETURN t

Updating date field using Hibernate - Row not updated if date is the same, time is different

I have a situation where a table in the database has a date field defined as date where time also is important (for sorting later).
At first, all times for the date where coming as 000000 but I updated the code to use timestamp and when inserting new records, it's working fine.
Update on the other hand will not change the database if the date is the same (but different time). Apparently, while inserting, hibernate doesn't take into consideration the time and the record is not change (or at least this is what I discovered from my testing).
I can't change the database structure to use timestamp or add a time field.
Any help is really appreciated :)
Thanks

Resources