set a due date dynamic - laravel

I would like to set a due date (dynamic), the person can register if it is 17to 60 years old.
I have tried this:
'date_naissance' => 'required|date|after:1960|before:2003-01-01'
But, I have to change the values each years.It is possible to improve this?
Thank you

You can try something like this:
$year = (int)date("Y");
'date_naissance' =>
'required|date|after:'.($year - 59).'|before:'.($year - 16).'-01-01'
Or a bit more precise:
'date_naissance' =>
'required|date|after:'.date('Y-m-d', strtotime('-59 year')).'|before:'.date('Y-m-d', strtotime('-16 year'))
It should work also with this:
'date_naissance' =>
'required|date|after:-59 year|before:-16 year'))

You would need to make custom validation rule. Here is example how to do it:
LINK
You will need to make method that takes person birth date and calculate his age. If age is greater then 17 it should return true.

Related

Laravel Eloquent date_format Validation fails on given format

I want to create validation rule to validate incoming date.
Format I want to validate is Y-m-d H:i:s. Here's my body request which I am validating:
{ "date":"2015.10.5 10:30:10" }
And here's my validation rule:
'date' => 'required|date_format:"Y.m.d H:i:s"',
And it returns:
{"date":["The date does not match the format Y.m.d H:i:s."]}
If you want to be able to pass the day without a leading zero, then for the day part of your datetime you need to use j instead of d.
'date' => 'required|date_format:"Y.m.j H:i:s"',
That will work for the example you have above.
If you are going to always have a leading zero in your day (so, 05 instead of just 5, then the date format you already have will work.

Finding Records From This Month Datamapper with Sinatra

I'm building a little Sinatra web app at the moment and having problems understanding queries with DataMapper.
I have a field in my database which I've called date, I'd love to query this and only return records from this current month.
I've tried this:
query = "2012-11"
#trips = Trip.all(:date.like => '%query%')
Which doesn't seem to work, I'm hoping someone might spot my error and give me some guidance! Hope someone can help, thanks for taking the time to read.
Dave
Provide a range instead:
#trips = Trip.all(:date => (query..query))
I'd also fix your date format and provide month, day and year.
you could try this:
require 'date'
date_c = Date.today
next_y, next_m = date_c.month == 12 ?
[date_c.year+1, 1] : [date_c.year, date_c.month+1]
date_a = Date.new(date_c.year, date_c.month)
date_z = Date.new(next_y, next_m)
Trip.all :date => date_a..date_z
this will return current month entries

How can I group on the created_at field and return only the Date and hour

I run the following in Rails:
coe = trackers.where(:trackable_type => "Mailing").count(:group => 'DATE(created_at)')
which returns a hash with the items nicely grouped.
However I want to group not by date only by date but by the hour, but not the minutes or seconds.
How can I run this query so all the heavy lifting is done by Postgresql and not convert after I get the data set back?
My mistake I want to group by Date and Hour. Sorry for the confusion.
It sounds like you want the date_trunc() function:
http://www.postgresql.org/docs/current/interactive/functions-datetime.html#FUNCTIONS-DATETIME-TABLE
I want to group not by date but by the hour, but not the minutes or
seconds.
Bold emphasis mine.
So date_trunc()is probably not what you want, but EXTRACT or date_part():
coe = trackers.where(:trackable_type => "Mailing").count(:group => 'EXTRACT (hour FROM created_at)')

Comparing Dates, ignoring year - Linq/Entity Framework

Is there any easy way to compare dates, that ignores year, using Linq and the Entity Framework?
Say I have the following
var result = context.SomeEntity.Where(e => e.SomeDate > startDate);
This is assuming that SomeDate and startDate are .NET DateTime's.
What I would like to do is compare these dates without comparing year. SomeDate can be any year. Is there any easy way to do this? The only way I could think of would be to use the following:
var result = context.SomeEntity(e =>
e.SomeDate.Month > startDate.Month ||
(e.SomeDate.Month == startDate.Month && e.SomeDate.Day >= startDate));
This method quickly gets more complicated if I am looking to have an endDate as well, as I will have to do things like take account for when the start date is at the end of the year and the end date is at the beginning.
Is there any easy way to go about this?
Update:
I ended up just going about it the way I had initially thought in the post... a heck of a lot of code for something conceptually simple. Basically just had to find if a date fell within a range, ignoring year, and looping the calendar if startDate > endDate If anyone knows an easier way, please post as I am still interested.
If you really need to compare only dates (not times) then DateTime.DayOfYear property might help. But you should be careful regarding leap years in this case. Other of this I cannot imagine anything more simple than your approach with comparing months and days.
If all you care about is that this method will become more complicated after introducing second comparison then simple method extraction should help.
Another approach might be creating an extension method which will return a number applicable for your comparison. For example let's call this method GetYearIgnoringOrdinal():
public static int GetYearIgnoringOrdinal(this DateTime date)
{
return date.Month*100 + date.Day;
}
And then use it like this:
var result = context.SomeEntity.Where(e => e.SomeDate.GetYearIgnoringOrdinal() > startDate.GetYearIgnoringOrdinal());
Slightly simpler looking way
var result = context.SomeEntity(e =>
e.SomeDate.Month * 100 + e.SomeDate.Day > startDate.Month * 100 + startDate.Day
);
You could also create a user defined function (assuming SQL server is used) and that function can be used in the query.

How to validate a price (money amount) mySql decimal column in Yii?

I am using a mySql DECIMAL(12,4) column to hold price values (seeing how that's what Magento uses). I want to validate them in my ActiveRecord model using Yii's CValidator rules, but I'm not quite sure how to do it.
I assume I can do it with the CTypeValidator set to "float", but I wanted to see how other folks are doing this. I don't see an actual "currency" validator. Maybe I should just validate the length?
array('price', 'type', 'type'=>'float'),
or
array('price', 'length', 'max'=>17), // 12 + 4 + . = 17?
Suggestions and examples? Thanks!
I myself used the:
array('price', 'type', 'type'=>'float'),
rule... if needed, you can also use or combine the previous with the 'match' validator
array('price', 'match', 'pattern'=>'fancy regex magic here'),
To add a working example, as this is common question (with no good answer in SO)"
public function rules(){
...
array('price', 'match', 'pattern'=>'/^[0-9]{1,12}(\.[0-9]{0,4})?$/'),
...
where {1,12} is the range of whole digits , and {0,4} is the range of "sub" units.
For a normal price range of 0.01 to 9999.99 use the regex like this:
'/^[0-9]{1,0}(\.[0-9]{0,2})?$/'
ref: kitune in Yii forums
For number validation you can use:
array('price', 'numerical', 'integerOnly' => false, 'min' => 0),
If you need to restrict digits before and after decimal mark then use regex as others have suggested.

Resources