I am using Laravel Framework 6.16.0 and I am creating from a string a date object so that I can input it in my db:
$transaction_date = Carbon::createFromFormat('Y-m-d', $tradeDate);
$filling_Date = Carbon::createFromFormat('Y-m-d H:i:s', $fillingDate, 'UTC');
$product = Product::updateOrCreate(
[
'price' => trim($price),
'qty' => $qty,
'companies_id' => $company->id,
'persons_id' => $person->id,
'amount_range' => $owned,
'filling_date' => $filling_Date,
'transaction_date' => $transaction_date,
],
[]
);
When running the above query my product does not get found as $filling_Date and $transaction_date are not matched in the database, even if my product already exists.
I am guessing, the reason is that I am creating a "new" Carbon object.
Any suggestions how to match the filling_date and transaction_date in the database?
I appreciate your replies!
Try this when converting a string to a date with Carbon
$date = Carbon::parse($yourStringDate);
Related
Hi i am using MongoDB as database in my cakephp application. and i am using this plugin for it. i want to search record(document) from my user collection. i am using this snippet
$userDetails = $this->User->find('all',array(
'conditions' => array(
'userId' => 18
)
));
it's working for me.
Now i wants to fetch the record by date like all user who was created in January Month of 2016. so for that i have to use similar to DATE_FORMAT function of MySQL and it might something like
$month = date('m');
$userDetails = $this->User->find('all',array(
'conditions' => array(
'userId' => 18,
'DATE_FORMAT(User.created_at,"%m") =' => $month
),
'fields' => array()
));
but how to use this type of Functionality with mongoDB can any one help me please?
thanks.
Try this :
var start = new Date(2016, 01, 01);
var end = new Date(2016, 01, 31);
db.User.find({userId: 18, created_at: {$gte: start, $lt: end}});
Source : here
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'm trying to select the values that fall between 2 dates, so I'll have to use <= and >=, however, my query is seemingly behaving as just less than or greater than and not considering values equal to the date.
I'm using CakePHP which saves dates in "Y-m-d h:i:a" format. I wanted to find dates on given week intervals (starting on Sundays), so I used the following.
$start_date = date('Y/m/d', strtotime('last Sunday', strtotime($timestamp)));
$formatted_start_date = str_replace("/","-",$start_date);
I tried to do find $start_date formatted as "Y-m-d" but then it wouldn't find the correct date, so I switched it to the way it is and used str_replace to format it to using "-" instead of "/".
$date_query = $this->Order->query("select * from orders where id = '$id' and created => '$formatted_start_date' and created <= '$current_timestamp' ");
Considering the time values in my database are in "Y-m-d h:i:a" format, can I use "Y-m-d" for date comparison? Is it possible to do a MySQL query that involves both LIKE and <= ?
No need to do a str_replace() - just get the Y-m-d:
$start_date = date('Y-m-d', strtotime('last Sunday', strtotime($timestamp)));
Then, instead of manually creating a query, use the CakePHP conventions (yes, you can use Y-m-d for date comparison even though the datetimes stored in the database are Y-m-d H:i:s)
$this->Order->find('all', array(
'conditions' => array(
'id' => $id,
'created >=' => $start_date,
'created <=' => $end_date . ' 23:59:59',
'my_field LIKE' => '%whatever%'
));
Though - this seems kind of strange - usually you're either looking for something by 'id' OR by a date range - not both. But - maybe you have a reason :)
And as you can see above, yes, you can add a 'LIKE' also if you need.
Above answer is totally correct, but you can take easier approach using cakePHP time helper, which has function daysAsSql, it transcribes and time-readable strings into database range.
http://book.cakephp.org/2.0/en/core-libraries/helpers/time.html#TimeHelper::daysAsSql
Just add condition's array like this.
$resl = $this->DBNAME->find('all',array(conditions=>array('date1>date','date1<date')));
Replace 'date' with your date.
This is worked for me.
Try this in your controller
$searchTutorQuery = $this->Tutordetails->find('all', array(
'conditions' => array(
"User.zip" => $zipcode1,
"Tutordetails.user_id"=>$uid,
"Tutordetails.subject_id" => $subjectName,
"Tutordetails.hourly_rate >=" => $hourly_rate
),
//"User.id =" => $userid,
'fields' => array('user_id', 'Tutordetails.subject_id', 'Tutordetails.hourly_rate',
'User.zip', 'Tutordetails.zip'),
'order' => array('Tutordetails.id DESC')
));
I have numerous documents containing a field called "date" which is simply a unix timestamp.
whithin lithium, i want to find all documents in a given date range. i'm currently trying the following:
//$_stats contains two \DateTime objects which are properly initialized
$transactions = Transactions::all(
array('conditions' => array(
'tags' => array('$all' => array((string)$tag->_id)),
'date' => array('$gte' => array((int)$_stats['date_start']->getTimestamp()), '$lte' => array((int)$_stats['date_end']->getTimestamp()))
))
);
But this returns zero documents. When I remove the "date" condition, it works fine and I get all documents.
What am I missing?
Thanks, aenogym
There doesn't seem to be any need of giving an array of dates, so perhaps try:
$transactions = Transactions::all(
array('conditions' => array(
'tags' => array('$all' => array((string)$tag->_id)),
'date' => array('$gte' => (int)$_stats['date_start']->getTimestamp(), '$lte' => (int)$_stats['date_end']->getTimestamp())
))
);
Keep in mind that MongoDate stores dates as miliseconds while timestamp uses seconds. In other words MongoDate has higher precision.
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.'`';