1

In php

echo time();

and you get some 10 digit time stamp.

in python..

import time
>>> print time.time()
1374872354.62

where is the PHP's time() equivalence in Python ?

user2624103
  • 49
  • 1
  • 5

2 Answers2

1

Python uses a float type so that it can represent fractional time. Just cast away that part:

>>> import time
>>> int(time.time())
1374872983
tdelaney
  • 73,364
  • 6
  • 83
  • 116
0

Ok so python's time() comes from the time module :)

which may or may not give the best time for your system:

time()

"Return the time in seconds since the epoch as a floating point number. Note that even though the time is always returned as a floating point number, not all systems provide time with a better precision than 1 second. While this function normally returns non-decreasing values, it can return a lower value than a previous call if the system clock has been set back between the two calls." - Python Time doc

sometimes clock() may work better:

"On Unix, return the current processor time as a floating point number expressed in seconds. The precision, and in fact the very definition of the meaning of “processor time”, depends on that of the C function of the same name, but in any case, this is the function to use for benchmarking Python or timing algorithms. - Python Time doc "

depending on your use you should choose one of these: for a discussion see here: Python - time.clock() vs. time.time() - accuracy?

PHP time according to the docs does the following:

"Returns the current time measured in the number of seconds since the Unix Epoch (January 1 1970 00:00:00 GMT). " -- PHP docs

So no matter which one you choose; you're most likely to get the same if not better resolution than PHP's time :)

If you want to time a function though, you should try the timeitmodule which automatically chooses from either clock or time depending on the OS granularity

Community
  • 1
  • 1
Eiyrioü von Kauyf
  • 4,481
  • 7
  • 32
  • 41
  • Although this is probably not the correct answer, i choose it because it looks like a-lot of effort was done to create this answer. - Sumer Kolcak – user2624103 Jul 26 '13 at 21:22
  • if you want higher precision in time for php use `microtime(true);` and get a float representation of the microseconds that have occured since the epoch. (which is 3 decimal places more precise then the millisecond measure shown in the example) – Orangepill Jul 26 '13 at 21:30
  • yup :). that is the most accurate way in PHP _assuming the hardware supports it_ "microtime() returns the current Unix timestamp with microseconds. This function is only available on operating systems that support the gettimeofday() system call." -- http://php.net/manual/en/function.microtime.php ;) – Eiyrioü von Kauyf Jul 26 '13 at 21:35