PHP mktime() and microtime() equivalent in C#

c#

Solution

There are no direct equivalents, but they can easily be implemented... Since the UNIX timestamp is the number of seconds since January 1st 1970, it's easy to calculate :

public readonly DateTime UnixEpoch = new DateTime(1970, 1, 1);

// equivalent to PHP mktime :
public int GetUnixTimestamp(DateTime dt)
{
    TimeSpan span = dt - UnixEpoch;
    return (int)span.TotalSeconds;
}

For microtime, you can use the DateTime.Tick property (1 tick = 100 nanoseconds, so 10 ticks = 1 microsecond)

Problem

What is the equivalent of PHP mktime and microtime in C#?

Original source