delete item by date in mongodb using php - 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")

Related

set dateTime format and soapclient error

I need to set a dateTime in this specific date format:
2016-10-19T11:06:20.000+00:00
So I found that this one work:
'date'=> $dateStart->format('Y-m-d\Th:m:s')
this is from a array cell setting inside a loop. At the first level of an multi level array.
Before this set, $dateStart is treathed like that:
$dateStart = new DateTime();
$dateStart->setTimeStamp($inputs['extraData']['dataStart']/1000);
And the var $inputs['extraData']['dataStart'] contain an UnixTimeStamp as this one:
1493828407000
All the time stamp are correct in the right ascending order.
In a next level of the same array I try to set another timestamp in this way:
$timeStamp=$cellaViaggio['timestamp'];
$date3= new DateTime();
$date3->setTimestamp($timeStamp/1000)->format('Y-m-d\Th:m:s');
then inside the array
array('name'=>'TIMESTAMP','value'=>$date3)
then this array is used as soap Data envelope in a call made by PHP Soap client.
If I leave all like that my Laravel rise an error of:
ErrorException in TripController.php line 37:
Object of class DateTime could not be converted to string
Otherwise if I leave all like that but I move the format command ( ->format('Y-m-d\Th:m:s') ) inside the array cell writing like that:
array('name'=>'TIMESTAMP','value'=>$date3->format('Y-m-d\Th:m:s'))
this no raise any error but the timestamp is wrong, because the minutes will not change and just second are increased at every loop, but not the minutes!
So something like about the time:
00:01:30
00:01:40
00:01:59
00:01:33
oo:01:03
As I said we talk about progressive timestamp.
Keep in mind that the timestamp is assigned to an Array of Array of Array to keep the soap envelope in the right format for the server.
this is the code for the SOAP client:
$client = new \SoapClient("http://soapserviceserver.xx/services/spPushDataServicePort?wsdl");
try {
return $cli->pushData($param);
} catch (SoapFault $e) {
return $e->getMessage();
}
$params contains the array set as described above.
The format is not proper because you are using
$date3->format('Y-m-d\Th:m:s')
Instead you should use
$date3->format('Y-m-d\Th:i:s')
Here is an example
\Carbon\Carbon::now()->format('Y-m-d\Th:i:s')
Result
2017-06-13T06:44:45
result after some time 2017-06-13T06:45:24
Read more about PHP Date formats here
i : Minutes with leading zeros
If you want perfect Date format according to the example given in the question then you can use
date(DATE_ATOM)
Result 2017-06-13T18:51:08+05:30

How to count objects with a particular day in mongodb and 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.

Converting string to date or getting the current date using Zend PHP Framework 2

I have a controller action in which I fetch the currrent date like following:
$fullDate=date("Y-m-d");
$date = strtotime($fullDate);
$viewModel->setVariable("currentDate",$date);
// Here I would like to pass the variable to the View and then compare it with another date ...
With the method above I'm getting this when I do var_dump($currentDate);
int(1463587200)
which is not what I was expecting... I need a date in format like this 2016-05-19 (year-month-day)... How can I do this the valid way???
This strtotime is converting your formatted date into a Unix time-stamp. just remove it.
$fullDate=date("Y-m-d");
$viewModel->setVariable("currentDate",$fullDate);
you'll be able to compare two dates in this format Y-m-d without converting to time-stamp.

php not outputing a ISO timestamp from mongoDB the format I need

I need to have a time stamp that is like this: 2008-01-28T20:24:17Z for a jQuery plugin however when I try to retrieve the date from a mongoDB using:
<?php print date('c', $article['created_at']->sec);?>
I get this back: 2012-01-24T19:20:12+00:00
Does anyone know how to change the timestamp to match the first ones formatting.
print date('Y-m-d\TH:i:s\Z', $article['created_at']->sec);
Both forms are valid for ISO 8601 dates though, if a program can parse one, it should be able to parse the other as well.

How to format a getUpdatedAt() kind of date in Symfony?

I'd like to change the formatting of a date in Symfony 1.4
The default one being:
<?php echo $question->getUpdatedAt();
// Returns 2010-01-26 16:23:53
?>
I'd like my date to be formatted like so: 26/01/2010 - 16h23
I tried using the format_date helper DateHelper class.
Unfortunately the API is rather empty (something really needs to be done about it.)
Browsing the helper's source code, I found that a second argument, format, can be passed.
I assumed it was using the same syntax as PHP's date function.
But here's what it outputs (same example as above):
<?php sfContext::getInstance()->getConfiguration()->loadHelpers('Date');
// [...]
echo format_date($question->getUpdatedAt(),'d/m/y - H\hi')
// Returns 26/23/2010 - 16\4i
I'm sure I'm not the first one having trouble doing this but I've been Googling around and nothing accurate showed up.
Do you guys have any idea how to format a date in Symfony 1.4?
Have a look at the new functions in 1.4.
You can do:
$question->getDateTimeObject('updated_at')->format('d.m.Y');
// I assume the field's name is 'updated_at'
From the docs:
Date Setters and Getters
We've added two new methods for retrieving Doctrine date or timestamp values as PHP DateTime object instances.
echo $article->getDateTimeObject('created_at')->format('m/d/Y');
You can also set a dates value by simply calling the setDateTimeObject method and passing a valid DateTime instance.
$article->setDateTimeObject('created_at', new DateTime('09/01/1985'));
But it seems only to work for Doctrine.
How bout going with the default PHP date function? date('d/m/Y', strtotime($question->getUpdatedAt())
You can use also sfDateFormat class to work with dates.
link text
do you try :
echo $question->getUpdatedAt('d/m/y - H\hi')
I think it's the easiest way

Categories