Difference between time () and new MongoDate()? - php

Is there a difference between using PHPs time() and using new MongoDate()?
I need to store created_at and updated_at dates for each documents in by mongoDB collection
so that I can query them by date (for example documents updated last week).
From what I can see time() and new MongoDate() produces the same result?

That's because time() is default for the MongoDate constructor, from the manual:
public MongoDate::__construct ([ int $sec = time() [, int $usec = 0 ]] )
You should use MongoDate objects to query MongoDB.
If you use the raw output of time(), you will store/query for an integer. When you use MongoDate, you will be using MongoDB's Date type which gives you some additional benefits.

Related

Maximum int value result function time()

I need to save the value returned by the php "time();" function in a mysql table.
When I create an int field within a table, the maximum length of the int value is requested.
I wanted to ask you what value suggest to include in the type declaration of int.
I ask you this because, by saving the digit in int, it will be easy to compare it with other results of the "time ();" (which if I understand correctly, it always returns an int value).
Thank you
As of PHP manual:
Returns the current time measured in the number of seconds since the Unix Epoch (January 1 1970 00:00:00 GMT).
So you're better of with using DATETIME type in your MySQL database and saving the timestamp after you convert it into valid format. For that I'd use DateTime. Even better if you just use new DateTime instead of time(), as DateTime takes time in constructor, which by default is now:
public DateTime::__construct ([ string $time = "now" [, DateTimeZone $timezone = NULL ]] )
If you were to use Doctrine 2, it would have been enough. Otherwise, you must format your DateTime object before inserting:
$dt = new DateTime();
$dt->format('Y-m-d H:i:s');
This will produce something like
2019-04-30 15:34:16
which is completely valid format for your DATETIME type in your mysql database.
If you need to compare other DateTime objects, you can always use DateTime::diff:
public DateTime::diff ( DateTimeInterface $datetime2 [, bool $absolute = FALSE ] ) : DateInterval
This will be enough for smart and representable operations with DateTimes and their comparison.
EDIT:
As you wrote
Ok, I converted the db field into a "DATETIME" field. But now I can't compare the result of the table field with a DateTime () object. Do you know how I can convert the table field to be comparable with a DateTime () object?
You need to create the DateTime object from the data you have in your database. It is achieved by using createFromFormat:
public static DateTime::createFromFormat ( string $format , string $time [, DateTimeZone $timezone ] ) : DateTime
So what you do is:
$dt = DateTime::createFromFormat('Y-m-d H:i:s', '2019-04-30 15:34:16');
and voila!

Storing and retrieving dates in Mongodb 3.2 with PHPLIB

Using Mongo and PHP how does one store a datetime type, and then retrieve by date?
This is what I have for inserting, but it stores the date as a string:
$collection->insertOne(['date_created' => '2017-01-02 17:20:15']);
How should I be sending the date to Mongo to store as a datetime type so I can do the proper finds?
Put an instance of this class into the array: http://php.net/manual/en/class.mongodb-bson-utcdatetime.php
You need to use the BSON objects in the PHP arrays: http://php.net/manual/en/book.bson.php
Those get serialized properly before passed to Mongo.
$test = array(
"_id" => 123,
"name" => "This is an example",
"date_created" => new MongoDB\BSON\UTCDateTime(time() * 1000)
);
$collection->insert($test);
The time() is multiplied by 1000, because the constructor wants the milliseconds elapsed since the unix epoch, and time() returns the seconds elapsed since the unix epoch. See: http://php.net/manual/en/mongodb-bson-utcdatetime.construct.php
UPDATE:
To retrive the date/time in ISO format, just convert the UTCDateTime object fist to DateTime:
$date_created = $test["date_created"];
$iso_date = $date_created->toDateTime()->format("Y-m-d H:i:s");

Php Mongodate Converting Date to 1970

Hello guys I 'm storing date in mongodb. The problem that I'm facing is that the date time string that I get. I try to convert it into mongodate but it converts it to 0.00000000 2016. Here is the code
$params['start'] = new MongoDate($params['start']);
$params['end'] = new MongoDate($params['end']);
The string bring the date time in this form 2016-04-07 19:49:50 but after the conversion it becomes like this 0.00000000 2016. Please tell me what is it that I'm doing wrong
Per the docs, MongoDate expects a timestamp value like 1460058590, not a string like 2016-04-07 19:49:50.
$params['start'] = new MongoDate(strtotime($params['start']));
The MongoDate constructor expects the time in Unix epoch seconds, not a time string.
public MongoDate::__construct ([ int $sec = time() [, int $usec = 0 ]] )
You'll need to convert your time string using strtotime or DateTime. The example code from the constructor documentation even includes an example:
$d = new MongoDate(strtotime("2009-05-01 00:00:01"));
echo "$d\n";

Add two times of different format in PHP

In my table, I have a variable $duration which is stored in minutes.
And I also have a time variable. Let it be $time1.
$time1=date('H:i:s',$time);
$duration=1000; //minutes
$time2= secondsToTime($duration*60);
I convert the $duration to time format using the function given below.
function secondsToTime($seconds) {
$dtF = new DateTime("#0");
$dtT = new DateTime("#$seconds");
return $dtF->diff($dtT)->format('%h:%i:%s');
}
So in $time2, i have something like this stored 11:12:13
And in $time1 i have something like this stored 01:10:19
I want to perform $total=$time1+$time2;
So, I converted $time2 into time format.
$timeform= new DateTime($time2);
$newtime2= $timeform->format('H:i:s');
Now, I add $total=$time1+$newtime2;
But echo date('H:i:s',$total);gave me following error:
Notice: Object of class DateTime could not be converted to int
The second argument of date() should be a timestamp (i.e. an integer, i.e. seconds), not a formatted date string.
As far as I know only comparison operators work on datetime objects ($date1 > $date2), not math operators ($date1 + $date2).
See also http://nl3.php.net/manual/en/datetime.add.php and http://nl3.php.net/manual/en/datetime.modify.php
Use 1 datetime instance for calculating/formatting the total amount of time
Or, convert 2 datetime instances to seconds, add them, and format using date()
The second parameter of date() is expected to be a Unix timestamp as integer, not a DateTime object. (see php.net)
You need to convert your DateTime object into a Unix timestamp.
Try getTimestamp():
echo date('H:i:s',$total->getTimestamp());

How to return ISO date format in PHP for MongoDB?

I want to store the current date generated from PHP into MongoDB collection as an ISO date formate.
ISODate("2012-11-02T08:40:12.569Z")
However I am not able to generate such Kind of date in php which will be stored in MongoDB as an ISODate format.
This is what I ve done.
$d = new MongoDate(time());
echo $d;
and it is outputting something like,
0.00000000 1353305590
which is not the format I need. How to do this?
You could run the __toString function, or use the sec field
__toString will return a timestamp in usecs, which you can pass to date() after separating the seconds from milliseconds - read here: http://us1.php.net/manual/en/mongodate.tostring.php
OR, I personally prefer to have mongodb return just the seconds, which can be plugged directly into date() - read here: http://php.net/manual/en/class.mongodate.php
Also, if you're generating a MongoDate() for right now, you don't need to specify time();
In order to return an isodate, you need to do this:
echo date(DATE_ISO8601, (new MongoDate())->sec);
...
$exampleDate = new MongoDate();
echo date(DATE_ISO8601, $exampleDate->sec);
EDIT: To save your ISO date, you need to do the following:
$mongoDateObject = new MongoDate(strtotime("2012-11-02T08:40:12.569Z"));
For clarity, let's consider the following use case:
You need to convert a string in the simplified extended ISO 8601 format (e.g. returned by Javascript's Date.prototype.toISOString()) to and from PHP's MongoDate object, while preserving maximum precision during conversion.
In this format, the string is always 24 characters long: YYYY-MM-DDTHH:mm:ss.sssZ. The timezone is always zero UTC offset, as denoted by the suffix Z.
To keep milliseconds, we'll have to leverage PHP's DateTime object.
From string to MongoDate:
$stringDt = "2015-10-07T14:28:41.545Z";
Method 1 (using date_create_from_format):
$phpDt = date_create_from_format('Y-m-d\TH:i:s.uP', $stringDt);
$MongoDt = new \MongoDate($phpDt->getTimestamp(), $phpDt->format('u'));
Method 2 (using strtotime):
$MongoDt= new \MongoDate(strtotime ($stringDt),
1000*intval(substr($stringDt, -4, 3)) // cut msec portion, convert msec to usec
);
From MongoDate to string:
$MongoDt = new \MongoDate(); // let's take now for example
$stringDt =
substr(
(new \DateTime())
->setTimestamp($MongoDt->sec)
->setTimeZone(new \DateTimeZone('UTC'))
->format(\DateTime::ISO8601),
0, -5) // taking the beginning of DateTime::ISO8601-formatted string
.sprintf('.%03dZ', $MongoDt->usec / 1000); // adding msec portion, converting usec to msec
Hope this helps.
convert ISO date time in UTC date time here :
$timestamp = $quicky_created_date->__toString(); //ISO DATE Return form mongo database
$utcdatetime = new MongoDB\BSON\UTCDateTime($timestamp);
$datetime = $utcdatetime->toDateTime();
$time=$datetime->format(DATE_RSS);
$dateInUTC=$time;
$time = strtotime($dateInUTC.' UTC');
$dateInLocal = date("d M Y", $time);
echo $dateInLocal; die;
You can convert ISODate time by using below code.
* return ISO-8601 date format:YYYY-MM-DD'T'HH:mm:ss.sssXXX , for example: 2015-09-07T10:13:45.110-07:00 .
*/
date("Y-m-d\TH:i:s.000P", strtotime($date));

Categories