I am trying to create a get statements function and I have some problems defining the date range.
The data will be passed via get method in the following format: ?year=yyyy&month=mm
The relevant column of the table has datetime type (2012-02-01).
I checked the TimeHelper class and more particularly the TimeHelper::daysAsSql($begin, $end, $fieldName, $timezone = NULL) but i am not sure if it applies for this case.
I was thinking if i can create a date in the format of Y-m using the two variables year and month and then use it in the conditions for retrieving the data but I am sure there is a more efficient and proper way. Could anyone help?
$user_id = $Client['Client']['id'];
$year = $this->request['url']['year'];
$month = $this->request['url']['month'];
//$date_created = date('Y-m');
if (!empty($user_id) && (!empty($date_created))) {
$Reports = $this->Report->find('all', array(
'fields' => array('amount','currency','date_created','invoice_no'),
'conditions' => array(
'Report.client_id'=> $user_id,
'Report.date_created >=' => $date_created,
'Report.date_created <=' => $date_created
),
)
);
MONTH(dateColumn) Returns an integer that represents the month of the specified date.
YEAR(dateColumn) Returns an integer that represents the year of the specified date.
InCakephp use as
$user_id = $Client['Client']['id'];
$year = $this->request['url']['year'];
$month = $this->request['url']['month'];
//$date_created = date('Y-m');
if (!empty($user_id) && (!empty($date_created))) {
$Reports = $this->Report->find('all', array(
'fields' => array('amount','currency','date_created','invoice_no'),
'conditions' => array(
'Report.client_id '=> $user_id,
'MONTH(date_created ) >='=> $month,
'YEAR(date_created ) <=' => $year
),
)
);
Related
Forgive me, this is a long one. I want to give all the background for this question.
I am using DataTables server-side processing for a project. My goal is to utilize the echo function to echo the number of days between today's date and dates in a column of my database called CreatedDate, which is a DateTime type.
I was trying to get the basic functionality working outside of DataTables first, and the function below works as intended, and outputs an $current_date of the number of days between $date and $date as: "16 days live", "15 days live", etc.:
$sql = "SELECT CreatedDate FROM Estimates";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
$created_date = $row["CreatedDate"];
$date = new DateTime($created_date);
$current_date = new DateTime();
$diff = date_diff($date, $current_date);
echo $diff->format('%d days live');
}
} else {
echo "No results";
}
In my DataTables $columns array, I am trying to achieve the same effect:
// date variable
$created_date = 'CreatedDate';
$current_date = new DateTime();
// array of database columns which should be read and sent back to DataTables.
// the 'db' parameter represents the column name in the database, while the 'dt'
// parameter represents the DataTables column identifier.
$columns = array(
array( 'db' => 'Client', 'dt' => 0 ),
array( 'db' => 'EstimateNumber', 'dt' => 1 ),
array( 'db' => 'Status', 'dt' => 2 ),
array( 'db' => 'CurrentEstimateTotal', 'dt' => 3 ),
array(
'db' => $created_date,
'dt' => 4,
'formatter' =>
function ($created_date, $row) use ($current_date) {
$date = new DateTime($created_date);
$diff = date_diff($date, $current_date);
echo $diff->format('%d days live');
}
)
);
I keep recieving an error of:
PHP Notice: Trying to get property of non-object.
How can I fix this? I am having a hard time getting a var_dump() of the $created_date and $date variables within the DataTables array to see what the problem is.
For more information about DataTables server-side processing, I am using this template as a base for my DataTables script. The DataTables script also uses this helper class. Thank you in advance!
Just return your string from callback.
'formatter' =>
function ($created_date, $row) use ($current_date) {
$date = new DateTime($created_date);
$diff = date_diff($date, $current_date);
return $diff->format('%d days live'); //That will solve your problem
}
Here I did the query for subtract but the result am getting null value. Is this correct way for subtract two datetime in project?
Code:
$arguments = array(
array(
'$project' => array(
'updatetime_difference' => array('$subtract' =>array('ISODate(2015-05-30T10:01:58.000Z)','$update_date')),
)
)
);
$result = $db->aggregate($arguments);
I have a field in the database to store the date of birth of a user in time stamp format, now i want to add a search filter in which only that users are shown whose date of birth between the two min or max limits. for eg. minimum=20 years and maximum=60 years. How i can compare the time stamp date with these two parameters?
I tried out with wordpress wp_query like follows:
$current = time();
$age_min = strtotime(date('Y')-$_REQUEST['age_min']);
$age_max = strtotime(date('Y')-$_REQUEST['age_max']);
//echo $age_min.'==='.$age_max;
$age = array($age_min,$age_max);
$bars = explode(',',$_REQUEST['bars']);
$radius = $_REQUEST['radius'];
$args = array('orderby' => 'registered',
'meta_key' => 'active',
'meta_value' => '1',
'order' => 'ASC',
'count_total' => true,
'meta_query' => array(array('key' => 'date_of_birth',
'value' => $age,
'compare' => 'BETWEEN',
'type' => 'DECIMAL',),));
$user_query = new WP_User_Query(array($args,'exclude'=>array($user_id) ));
Convert the strtotime back to dates in Min and Max values :
I have converted back to Y-m-d you can convert it to your used DOB
$age_min = strtotime(date('Y')-$_REQUEST['age_min']);
$age_max = strtotime(date('Y')-$_REQUEST['age_max']);
//echo $age_min.'==='.$age_max;
$age_min = date('Y-m-d', $age_min);
$age_max = date('Y-m-d', $age_max);
And then pass it to arrays :
$age = array($age_min,$age_max);
A simple date format mm/dd/yyyy validation is all I need...
$rules = array(
'renewal_date' => array('required', 'date_format:?')
);
What do I set the date format to? The Laravel documentation could be so much better.
Documentation is pretty clear to me, you should use
date_format:format
"The field under validation must match the format defined according to the date_parse_from_format PHP function."
Looking at it: http://php.net/manual/en/function.date-parse-from-format.php, I see you can do something like this:
$rules = array(
'renewal_date' => array('required', 'date_format:"m/d/Y"')
);
This is pure PHP test for it:
print_r(date_parse_from_format("m/d/Y", "04/01/2013"));
You can also do it manually in Laravel to test:
$v = Validator::make(['date' => '09/26/13'], ['date' => 'date_format:"m/d/Y"']);
var_dump( $v->passes() );
To me it's printing
boolean true
I got a similar problem, but with a d/m/Y date format.
In my case, the problem was that I defined both "date" and "date_format" rules for the same field:
public static $rules = array(
'birthday' => 'required|date|date_format:"d/m/Y"',
...
The solution is to remove the "date" validator: you must not use both. Like this:
public static $rules = array(
'birthday' => 'required|date_format:"d/m/Y"',
...
after that, all is well.
Workaround:
'renewal_date' => array('required', 'date_format:m/d/Y', 'regex:/[0-9]{2}\/[0-9]{2}\/[0-9]{4}/')
You should use with double quote like "Y-m-d H:i:s"
$rules = array(
'renewal_date' => array('required', 'date_format:"m/d/Y"')
^ ^ this ones
);
Discussion about this issue on GitHub: https://github.com/laravel/laravel/pull/1192
date_format didn't work for me , so I did this custom validation
Validator::extend('customdate', function($attribute, $value, $parameters) {
$parsed_date = date_parse_from_format ( "Y-m-d" , $value);
$year = $parsed_date['year'];
$month = $parsed_date['month'];
$month = $month <= 9 ? "0" . $month : $month;
$day = $parsed_date['day'];
$day = $day <= 9 ? "0" . $day : $day;
return checkdate($month, $day, $year);
});
$validation = Validator::make(
array('date' => $num),
array('date' => 'customdate')
);
Use the PHP date_parse_from_format (Laravel 4):
'birthday' => 'date_format:m/d/Y'
Your validation message will also use "m/d/Y" which the average user will not understand.
The birthday does not match the format m/d/Y
Recommend customizing your message for this invalid response.
The birthday does not match the format mm/dd/yyyy
Source : Click Here
You can use like
$rules = [
'start_date' => 'date_format:d/m/Y|after:tomorrow',
'end_date' => 'date_format:d/m/Y|after:start_date',
];
I have the foll code as:
$table_project_win = new Application_Model_DbTable_AfterWinProject();
$data_win = array(
'project_id' => $project_id,
'project_name' => $project,
'project_type_id' => $pro_type,
'start_date' => $dateStart,
'end_date' => $dateEnd,
'project_size' => $size,
'project_description' => $pro_des
);
$table_project_win->insert($data_win);
Here I get the $dateStart and $dateEnd variabled using as:
$dateStartt = $this->_getParam('dateStart');
echo 'date Start: '.$dateStartt;
$dateStart='"'.$dateStartt.'"';
$dateEndd = $this->_getParam('dateEnd');
$dateEnd='"'.$dateEndd.'"'
By using getParam I get the value of the date that the user has given input But when i will insert it into the database I use as
$dateStart='"'.$dateStartt.'"';
$dateEnd='"'.$dateEndd.'"'
But in the database table the value for date inserted is '0000-00-00' While when I echo the $dateStart which I have got through getParam It gives the correct value as '2012-12-11'.What is the reason of it??What Should I do??
replace $dateStart='"'.$dateStartt.'"';
with
$dateStart= $dateStartt ;
or
$dateStart='`'.$dateStartt.'`';