Adapt datetime field in Yii automatically to right format - php

I'd like to have a field inside a form, which has a format like dd.MM.yyyy HH:mm. I've done this with the date validator.
But now I have the problem, that the user always have to enter a time, not only the specified day. Is it possible automatically set the time to 00:00 when the user only enters the date?
Thanks for your help.

protected function beforeSave()
{
if ( strpos(trim(this->date_field), ' ') !== false ) {
$this->date_field = sprintf('%s 00:00', $this->date_field);
}
return parent::beforeSave();
}

Related

Laravel nova diffForHumans DateTime

I have field last_active on users, I want display time with diffForHumans or time_from_now from Moment.js. How I can do it? Now I just use:
DateTime::make('Activiy', 'last_active')
->exceptOnForms(),
When I use:
DateTime::make('Activiy', 'last_active')->diffForHumans()
->exceptOnForms(),
I get undifined method diffForHumans.
To my knowledge at the moment DateTime only supports format.
Since you want only to display, you can try Text field & display the humanise value. If your column might be null be sure to check for that otherwise you will get an error.
Text::make('Activiy', 'last_active')
->displayUsing(function($lastActive) {
if ($lastActive === null) {
return null;
}
return $lastActive->diffForHumans();
})
->exceptOnForms(),

MySQL + PHP - Querying relative format dates

I have two tables: steps and flows.
steps has many flows.
steps has relative_time (string) column.
flows has active_on (nullable, timestamp) column.
Steps relative_time stores relative formats and their values looks like: "+1 year", "+2 days", "+30 days", etc.
Flows active_on is set when a flow gets activated by user and it looks like: "2017-12-30 00:00:00".
Every time I want to know when a flow will expire, I select it from database and, in PHP (Laravel, Carbon), I execute:
/**
* #return null|Carbon
*/
public function getExpiresAtAttribute()
{
if (!$this->active_on) {
return null;
}
// Access the relative time for step (for example: "+3 days").
$relativeTime = $this->step->relative_time;
// Adds the limit time to the activation date,
// turning it possible to check when the current flow
// will "expires".
return $this->active_on->modify($relativeTime);
}
Problem
It's easy to check expires at value in PHP. The problem is that now I need to select only flows that are "expired" directly from database and I don't know if it is possible by using this approach. How can I achieve this?
You could store in relative_time the number of days, instead of the php date interval. This way you can query:
SELECT * FROM flows, steps WHERE flows.step_id = step.id AND NOW() > ADDDATE(flows.active_on, steps.relative_time)
This way you get all the flows expired.
No need to change database structure actually. You could create a migration to transform relative_time from dateinterval to number of days (is a string field).
foreach (Steps::all() as $step) {
$step->update([
'relative_time' => strtotime($step->relative_time,0)/(3600*24);
]);
}
Then you can adjust the getExpiresAtAttribute:
/**
* #return null|Carbon
*/
public function getExpiresAtAttribute()
{
if (!$this->active_on) {
return null;
}
// Access the relative time for step (for example: "+3 days").
$relativeTime = $this->step->relative_time;
// Adds the limit time to the activation date,
// turning it possible to check when the current flow
// will "expires".
return $this->active_on->modify("+$relativeTime days");
}

Date format issue on admin pannel

I have a website on magento framework. When i logged in admin panel click on Customer > Manage customer then all the customer detail are showing with ( Name, Email, Avtar, Group Phone No , ZIP, Country, State, Customer since, Action )
When user sign up on 2 OCT 2015 then the date is showing in Customer since is Feb, 10,2015. It is picking month as a date and date as a month.
Will you please tell me how can i change the format of Customer Since tab
try this
magento have date format in app/code/core/Mage/core/Model/Date.php file. if you want to change in format you can do that by overriding the model.
Note : never modify core files
I closely looking the issue and found that your month and date are swapping with each other.
try this
You can copy the file to \app\code\local\Mage\Eav\Model\Entity\Attribute\Backend\Time\Created.php and modify the functions necessary, once modified and uploaded it will take precedence over the core file and no worries about being overwritten during an update (though, if they update and that file is part of it, I would hope it would be to fix the problem)
the update to that file is here, so nobody needs to hop pages to find it
Replace beforeSave with this:
public function beforeSave($object)
{
$attributeCode = $this->getAttribute()->getAttributeCode();
$date = $object->getData($attributeCode);
if (is_null($date)) {
if ($object->isObjectNew()) {
$object->setData($attributeCode, Varien_Date::now());
}
} else {
// ADD THIS
$date = strtotime($date);
// convert to UTC
$zendDate = Mage::app()->getLocale()->utcDate(null, $date, true, $this->_getFormat($date));
$object->setData($attributeCode, $zendDate->getIso());
}
return $this;
}
This also needs to be corrected on the way out of the database. Replace afterLoad with this:
public function afterLoad($object)
{
$attributeCode = $this->getAttribute()->getAttributeCode();
$date = $object->getData($attributeCode);
// ADD THIS
if (!is_null($date)) {
$date = strtotime($date);
}
$zendDate = Mage::app()->getLocale()->storeDate(null, $date, true, $this->_getFormat($date));
$object->setData($attributeCode, $zendDate->getIso());
parent::afterLoad($object);
return $this;
}
I think this might help you.

Date Format issue in CGridView - YII Framework

I am trying to display created date column in CGridView, in DB its datatype is DateTime.
I have used the below code to format the date,
array( 'name'=>'created',
'header'=>'created',
'value' => 'Yii::app()->dateFormatter->format("d/M/y",strtotime($data->created))'
),
I am getting date formated column in CGridView, but null values shows sysdate in column. Please see attached image,
Please provide any solution to remove sysdate in null values column
Well, strtotime(null) will return false (Kami is apparently wrong).
The issue is in Yii date formater : Yii::app()->dateFormatter->format("d/M/y", false) will return current date.
You should simply create a getter in your model :
function getCreatedDate()
{
if ($this->created===null)
return;
return Yii::app()->dateFormatter->format("d/M/y", $this->created);
}
And in your grid view columns, you just have to use :
array('name'=>'created', 'value'=>'$data->createdDate'),
or 'createdDate'.
Use nullDisplay property of CGridView and set:
'nullDisplay'=>''

CodeIgniter: TankAuth, Check user date of birth as a callback

Ok, So I ave implemented a Date of Birth into the user registration. What I want to do now is take that date of birth and check to see if they are above a certain age (13) before they register. They way I did DOB is kinda weird, but it works. I have 3 field dob1, dob2, dob3. CodeIgniter: Tank Auth, Adding Date of Birth Issues here is how i implemented it if anyone is interested. Anyway, this is what I have been trying so far: EDIT: the syntax the user puts in is mm dd yyyy
function is_old_enough($input, $dob1, $dob2) {
$dob = $dob1.$dob2.$input;
$date = date('md').(date('Y')-13);
if ((int)$dob < (int)$date)
$this->form_validation->set_message('is_old_enough', 'You are not old enough to have an account on this site.');
return $input;
}
And here is what is inside the register() function.
$this->form_validation->set_rules('dob1', 'Date of Birth Month', 'trim|required|xss_clean|exact_length[2]');
$this->form_validation->set_rules('dob2', 'Date of Birth Day', 'trim|required|xss_clean|exact_length[2]');
$this->form_validation->set_rules('dob3', 'Date of Birth Year', 'trim|required|xss_clean|exact_length[4]|callback_is_old_enough[dob1||dob2]');
Am I close? Am i way off? Anyone help? Right now all it does is pretends like I never created this callback and puts the user in even if the user is too young. I know it is calling the function correctly as I had some issues with the variables. Help?
EDIT: Brendan's answer helped me a lot, but the main issue was a logic error. So here is how I have it working right now:
//Check if user is old enough
function is_old_enough($input) {
$dob = $this->input->post('dob3').$this->input->post('dob1').$this->input->post('dob2');
$date = (date('Y')-13).date('md');
if ((int)$dob > (int)$date) {
$this->form_validation->set_message('is_old_enough', 'You are not old enough to register on this site.');
return FALSE;
}
return TRUE;
}
$this->form_validation->set_rules('dob1', 'Date of Birth Month', 'trim|required|xss_clean|exact_length[2]');
$this->form_validation->set_rules('dob2', 'Date of Birth Day', 'trim|required|xss_clean|exact_length[2]');
$this->form_validation->set_rules('dob3', 'Date of Birth Year', 'trim|required|xss_clean|exact_length[4]|callback_is_old_enough[]');
First of all, you can only pass up to two arguments in a callback. Second, if you return a non-boolean value from a callback, whatever is returned will replace the value of the field you ran the callback on.
If you're trying to check if something is valid, the way it works (essentially) is:
function _callback_for_field($input)
{
// check if $input is valid based on your own logic
if($input == YOUR_LOGIC_HERE)
{
return TRUE;
}
return FALSE;
}
But for what you're doing, specifically:
// Birthdate rules
$this->form_validation->set_rules('birthdate-month','Birthdate Month','required|is_natural_no_zero|greater_than[0]|less_than[13]');
$this->form_validation->set_rules('birthdate-day','Birthdate Day','required|is_natural_no_zero|greater_than[0]|less_than[32]');
$this->form_validation->set_rules('birthdate-year','Birthdate Year','required|is_natural_no_zero|greater_than[1930]|less_than['.(date("Y") - 18).']');
I purposely don't go to great lengths to prevent someone that's barely under the age of 18 to register, because if they want to, they'll do it anyway. It is impossible to restrict registration based on age if the person is determined to, and since you're not checking some government database on citizens, it's not really within your scope of responsibility. A simple age check is all that's required.
I had a second thought -- If you still want to accurately check, you can reference $this->input->post() for each of the fields in your callback function. You can even run the callback function with no arguments, since you will be circumventing that restriction.

Categories