Thanks for the link, but when i use that approach $utcdatetime = new MongoDB\BSON\UTCDateTime($orig_date*1000); it change my date and also store it as date type in format 2021-01-25T07:25:59.765+00:00
For example, if my date is today’s date, then multiplying it with 1000 cause date change…
Also, type is Date but what if i want to store it as type Timestamp?
If you are referring to the BSON type Timestamp, don’t use that, stick with the Date type. If you are not then ignore the rest of this response.
A Date will give resolution down to the millsecond, a Timestamp is at second resolution with an ordinal counter. Primarily the Timestamp is used in a Mongodb replicaset members oplog.
BSON has a special timestamp type for internal MongoDB use and is not associated with the regular Date type. This internal timestamp type is a 64 bit value where:
the most significant 32 bits are a time_t value (seconds since the Unix epoch)
the least significant 32 bits are an incrementing ordinal for operations within a given second.
Note
The BSON timestamp type is for internal MongoDB use. For most cases, in application development, you will want to use the BSON date type. See Date for more information.
I realize this is an old post, but I am curious about the 32-bit time_t part of BSON Timestamp… Is it a signed or unsigned field? I ask because if it is signed, it will rollover on Jan 19, 2038 (due to the Y2038 issue). If it is unsigned, then it will not rollover until the year 2106. I also realize that the use of BSON Timestamp is intended for internal use only, but my concern is that there could be internal Y2038 issues, or stray uses of BSON Timestamp in applications that may be susceptible to Y2038 if the time_t field in BSON Timestamp is signed.