C# Code Snippets

PHP Time

Calculates the number of seconds since the Unix Epoch (January 1 1970 00:00:00 GMT).

Platform: .NET Framework 2.0

private long time()
{
    return time(DateTime.UtcNow);
}

private long time(DateTime time)
{
    DateTime unixEpoch = new DateTime(1970, 1, 1, 0, 0, 0);
    TimeSpan span = time.Subtract(unixEpoch);

    return (long)span.TotalSeconds;
}

private DateTime fromPHPTime(long ticks)
{
    DateTime unixEpoch = new DateTime(1970, 1, 1, 0, 0, 0);
    return unixEpoch.Add(new TimeSpan(0, 0, (int)ticks));
}

Back to C# Code Snippet List

Other C# Articles