How to count objects with a particular day in mongodb and php - php

I have a date saved as a string like this tdate:"2016-11-26 2:23:11" in mongodb. I want to count all objects which have this date 2016-11-26.
This is how I am trying to do using php but have no luck.
date_authenticated has this format "Y-m-d H:i:s" while I want to get all objects by day which should have this format "Y-m-d"
function count_auth($tdate) {
$collection = 'mycollection';
return $this->db->{$collection}->count(array('date_authenticated' => $tdate));
}

If you have saved the date as a string you can find entries with MongoRegex like described in PHP mongo find field starts with. In your case it should be something like:
$startsWithRegex = '/^2016-11-26/';
$this->db->{$collection}->count(['date_authenticated' => array('$regex'=>new MongoRegex($startsWithRegex))]);
However I recommend you reading Find objects between two dates MongoDB as you can perform better queries if you handle dates properly.

Related

How to use field DateTime with time zone in neo4j?

I am working with neo4j in PHP and I need to have a DateTime field that allows to store the time zone. If I save it as a string it is more difficult to make queries.
Add the timezone, and just output the string like this:
$x = new DateTime('2018-09-18 22:00:00', new DateTimeZone('Europe/Brussels'));
echo $x->format(DateTime::W3C);
Which gives you:
2018-09-18T22:00:00+02:00
When you pull your time from the DB, you can now create the object like this:
DateTime::createFromFormat(DateTime::W3C, $row['your_date_column']);
Play with it here: https://3v4l.org/DQjJO

Date from API call not being accepted in Laravel as a dateTime field

I am getting dates from an API call. The date is formatted in this way
2017-10-19T15:30:00
I want to store this date in my MYSQL database using Laravel Database Migration, currently I am using
$table->dateTime('datetime');
When I store it using a dateTime field as above, all I get is
0000-00-00 00:00:00
When I use a timestamp format, I don't get accurate dates, I just get the current time and date.
How can I solve this? Any help would be appreciated, and please let me know if you want further information.
Luckily, Laravel uses the Carbon class, which makes things a lot easier to modify dates. In your case, you want to do this:
Carbon::createFromFormat('Y-m-d\TH:i:s', $date);
There are two ways you can implement it: you can modify it before you save it to your database, or you can add a mutator on your model.
public function setDatetimeAttribute($value)
{
$this->attributes['datetime'] = Carbon::createFromFormat('Y-m-d\TH:i:s', $value);
}
You may want to build in some validation to see which format the date/time is in before you try to convert it.
in the model you should put:
protected $dates = ['datetime'];
Use Carbon
$dt = Carbon::parse('1975-05-21 22:23:00.123456');
to save:
$model = new Model;
$model->date = $dt; // you can use the carbon object directly
$model->save();

delete item by date in mongodb using php

I need to delete a mongodb element using php based on the date of the element
When i fetch the Date of the element without any formatting or conversion i get this as an output
0.52400000
I dont know which format this is but by using
date('Y-m-dH:i:s', $post["Date"]->sec)
this function i convert the date to human readable format which gives me something like this
2017-05-1210:23:022017-05-1210:38:102017-05-1210:24:58
now based on this value i want to delete an item in the mongodb collection... i.e that specific item which is having this timestamp..
Try this,
$date = new MongoDate(strtotime("2017-05-12 10:24:58"));
$collection->remove(array('Date' => $date));
Please refer these links - MongoDate and mongodb remove elements. And use MongoDate class all the time. Otherwise there can be conflicts.
EDIT
You can get your date objects' getTimestamp() insteads of strtotime("2017-05-12 10:24:58")

How to find the difference in days between two dates in laravel?

I have stored the date as a string in my DB in this format (dd-mm-yyyy).
Here I want to check the difference in days between the current date and the date in DB.
Here is my controller code:
public function index()
{
$domain_count = domain_details::get()->count();
//var_dump($domain_data);
$domain_alert = domain_details::
where('domain_ex_date','>',date('j-m-y'))
->get();
return view('home1')->with('domain_count' , $domain_count)
->with('domain_alert' , $domain_alert);
How do I achieve this? Is my approach right?
The above code shows 2016 is greater than 2017. I can see my logic is wrong but how do I change this?
It's better to have your dates in a DATE column in a proper format, otherwise MySQL won't know how to calculate it. Since you don't, you'll have to convert it with str_to_date, passing in the raw command:
where(DB::raw("str_to_date('domain_ex_date','%d-%m-%Y')"),'>',date('Y-m-d'))

Comparing two dates on PHP when date format on Database is "Apr 5 2013"

Hi everyone im stuck with this: I want to erase records on a database that are in the past. I just want to get into account Month and Day. For example, if the database record is (this is how is formatted on the DB) "Apr 5 2013" i need to compare it with today's date "Apr 6 2013". In this case, this record gets deleted. I´ve seen examples using UNIX timestamp, but none using that format using the date('M j Y'). Thanks!
The better way to store dates in databases is using DATE or DATETIME format. SQL allows you to get all informations you want from those types. But a request like this should work.
You could do it eventually with a regular expression, but it would be very heavy...
But you can do it in PHP, getting all results, then browsing them and comparing dates using something like strtotime, and then deleting every ID. Still very heavy but easier to implement than within a SQL request. But it still would be better if you could change the SQL architecture.
The approach I would try would be to convert the english text string into a unix timestamp using strtotime() then compare the new timestamp to the current date. Here is some php pseudo-code that describes the approach.
$threshold_to_deletion = numbers of days old a record is allowed to be;
$is_it_yesterday = 0;
$current_date = new DateTime();
$current_date->getTimestamp();
if ( !($is_it_yesterday = strtotime($str_from_db)) === false ) {
$date_interval = date_diff($is_it_yesterday, $current_date);
if( $date_interval >= $threshold_to_deletion ) {
Do stuff here to delete
}
} else {
echo "DB string is not a valid date: $str_from_db";
}
Please note it is not finished or tested, it's just there to loosely describe how it would be done. I hope this helps.
Tim Dumas
www.mpact-media.com

Categories