MODX Community

Date of Birth dob unix format on save

Hello, I’m using the login extra and I’m creating a hook to save the dob on unix timestamp for the registration and update details pages.

Using jquery datepicker to select the date in human format 01-01-1999
<input class="datepicker" type="text" name="dob" id="dob" value="" />
Then on submit I need the hook to save the info in unix to be displayed right in the manager users area.

my hook:

<?php
$dob = $hook->getValue('dob');
$unixDate = strtotime($dob);
$hook->setValue('dob', $unixDate);

return true;

I can see it’s working and converting the timestamp right:

$modx->log(xPDO::LOG_LEVEL_ERROR, $unixDate);

But it’s not being saved right, how can I replace the value to save it in the right format?

Thanks!

Are you running it in the preHooks or the postHooks? The preHooks run before a user is saved, and the postHooks run after.

Sorry mate, it’s a postHook because the form needs to be filled to have a value to modify!

Have you checked the value directly in the database? If it’s a timestamp, MODX will usually convert it to a human-readable date when you retrieve it with a tag or in code, which might make you think it’s stored in the wrong format.

Hello bob.
I’m trying to read and save the dob (date of birth) for users on registration and edit profile sections.

In the profile page (Manager Manage/Users) the date format is: 01/15/1988 for Jan 15 1988

If I try to save this format 01/15/1988 using jquery datepicker on registration or update profile, the date on the user manager page always is 01/01/1970. That means the date is not being saved right.

If I have a date already saved in the manager area and use the placeholder to display the dob: [[!+dob]]
it return the unix format, for example: 116809200

If I save the dob from one of the forms in unix format it works fine and save the right date.

That is the reason why I’m trying to modify the field date between validation and send.
In my mind I take the value, convert it to unix and save it should work fine.

and if I need to display it I only use the placeholder like this: [[!+dob:date=%d-%m-%Y]]

Please let me know what I’m doing wrong.

Thanks!

It appears that my earlier post was wrong. The dob field expects a unix timestamp.
If your JQuery datepicker gives you a human-readable date, you’ll have to call strtotime() on it before saving it in the dob field.

Yes exactly, but I think the best option is not to use the dob field instead use an extended field.
What do you think?

I think I would try to modify the result returned from your datepicker to turn it into a timestamp. This is one method. Since you know the format produced by the datepicker, though, you might be able to come up with something simpler.

Since you need a timestamp and the datepicker doesn’t give you one, I don’t see how using an extended field would help. To do that, you’d have to use PHP to get and write the extended fields array, and if you’re in PHP, why not just use strtotime() and put it in the DOB field?

You could use the jQuery datepicker options to change the format date to unix timestamp

$( "#datepicker" ).datepicker({ 
     dateFormat: '@' 
});

The above will also change the field to be unix though (displayed on the front end). If you don’t want that I think you can use parseDate function. Store the date into a variable and throw it into parseDate.

$.datepicker.parseDate( "@", chosenDate );

Yes mate I use it before, the problem was that it display the date in unix time, I even use normal time in one field and unix time in a hidden field, but at the end all that wasn’t necessary.

I solve th eproblem (the most stupid way).

This snippet:

<?php
$dob = $hook->getValue('dob');
$unixDate = strtotime($dob);
$hook->setValue('dob', $unixDate);

return true;

Needs to go in the &preHooks, and it works.

the problem before was that I was running it in the $postHook, so it didn’t setValue.
Thanks everybody for your help! :grin:

1 Like