lak91
lak91

Reputation: 1

How to get current timestamp in php?

I need to insert the current timestamp to mongodb in PHP. I wrote the following code:

date_default_timezone_set('UTC');
$end = new MongoDate(strtotime(date('Y-m-d H:i:s')));
echo $end;

I get the following output:

0.00000000 1379320378

Now I have two questions:

  1. This code is right?
  2. How can I get the output like:

1290932238757

Upvotes: 0

Views: 4715

Answers (2)

Sammaye
Sammaye

Reputation: 43884

The MongoDate object consists of two propeties and this is what you see when you echo, you actually see the string serialisation of the object in the form of the usec property. To get the result you want you can do:

$end->sec

Upvotes: 1

Mihai Iorga
Mihai Iorga

Reputation: 39704

Your desired output is a Unix Timestamp concatenated with Swatch Internet time

$timestamp = date('U');
$swatch = date('B');

$now = $timestamp.$swatch;
echo $now; // 1379320935404

Codepad example

Upvotes: 2

Related Questions