Jump to content
Krlos

Selector for birth date

Recommended Posts

Hello,

I suspect that this is very easy, but I can not find the solution.

I have created a custom field birth_date in my users template.

I'm trying to output users that are celabrating her/his birthday today, but I can't find a selector to do the job.

I was checking the data stored in the custom field and is like this: 1992-10-23 00:00:00

I'm using this selector:

$result = $users->find("birth_date~=today");

when I use that selector, it just output all users.

 

Share this post


Link to post
Share on other sites

Doesn't using the "=" operator work? Why use "~="? Maybe I'm not catching something.

Share this post


Link to post
Share on other sites
2 minutes ago, elabx said:

Doesn't using the "=" operator work? Why use "~="? Maybe I'm not catching something.

I have tried all selector operators, none of them work for me.

Share this post


Link to post
Share on other sites

Ohh! And just read it's a custom field! I was assuming a date field and I do wonder, why not use the default date field for this? I think the selector might not be working because it's not getting saved as a timestamp.

Share this post


Link to post
Share on other sites
6 minutes ago, elabx said:

Ohh! And just read it's a custom field! I was assuming a date field and I do wonder, why not use the default date field for this?

I'm using default datetime field.

In my users template I have:

date-1.jpg.7a784a4783e1a2a7de09fb7382b56b39.jpg

but in the custom field is stored:

date-2.jpg.f6d40130b7cf65e9b9551c0f4bfa7a59.jpg

I guess if I construct a date like (month-day):

$monthAndDay = "11-18"

should match 1962-11-18 00:00:00

but using selector (or any other operator):

$result = $users->find("birth_date~=monthAndDay");

it just output all users.

Share this post


Link to post
Share on other sites

Dates are stored as timestamps and you have to search the whole day :)

$start = strtotime(date('Y-m-d') . " 00:00:00");
$end = strtotime(date('Y-m-d') . " 23:59:59");
$result = $users->find("birth_date>=$start, birth_date<=$end");

 

  • Like 3

Share this post


Link to post
Share on other sites
56 minutes ago, maxf5 said:

Dates are stored as timestamps...

I don't think this is correct. If you are talking about FieldtypeDatetime, dates are stored as datetime. This is not the same as a timestamp 🙂; there are some 'little' differences. Sorry if I am being pedantic!

Edited by kongondo
  • Like 1

Share this post


Link to post
Share on other sites

A little self promotion: I just added "date" and "time" subfields to my DatetimeAdvanced replacement for PW's core Datetime field. That lets you search like this:

$today = date('y-m-d');
$result = $users->find("birth_date.date=$today");

Also works for in-memory (i.e. PageArray) searches.

  • Like 8

Share this post


Link to post
Share on other sites
14 hours ago, kongondo said:

I don't think this is correct. If you are talking about FieldtypeDatetime, dates are stored as datetime. This is not the same as a timestamp 🙂; there are some 'little' differences. Sorry if I am being pedantic!

no problem, thanks for clearing up.

I thought i would be the same as $page->created, $page->modified

Share this post


Link to post
Share on other sites
On 10/23/2018 at 2:45 PM, BitPoet said:

A little self promotion: I just added "date" and "time" subfields to my DatetimeAdvanced replacement for PW's core Datetime field. That lets you search like this:


$today = date('y-m-d');
$result = $users->find("birth_date.date=$today");

Also works for in-memory (i.e. PageArray) searches.

Thank you! this is working great.

But now I have request, how can I order based on day of birth.

I have this selector:

$result = $users->find("profile_birth_date.month=$mes, sort=profile_birth_date");

but is not sorting.

I tried adding .day to the profile_birth_date selector

but I got this error:

Error: Exception: SQLSTATE[42S22]: Column not found: 1054 Unknown column '_sort_profile_birth_date_day.day' in 'order clause' (in /home/xxxx/xxxx/wire/core/PageFinder.php line 413)

 

Share this post


Link to post
Share on other sites

I'm not sure if I can implement sorting directly in the database for advanced date parts, but you can sort in-memory.

$result = $users->find("profile_birth_date.month=$mes");
$result->sort('profile_birth_date.day');
// $result is now sorted

 

  • Like 1

Share this post


Link to post
Share on other sites
3 hours ago, BitPoet said:

I'm not sure if I can implement sorting directly in the database for advanced date parts, but you can sort in-memory.


$result = $users->find("profile_birth_date.month=$mes");
$result->sort('profile_birth_date.day');
// $result is now sorted

 

Thank you @BitPoet

It worked perfectly!

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...