I'm trying to create an accurate Unix timestamp to assign to a variable, based on different time periods. Any idea how these timestamps periods can be calculated?
I could only suggest to you to read the time Manual.
http://php.net/manual/en/function.time.php
Today to last midnight -> Get Hours + Minutes, do a Minus 24:00 hour calculation, use http://php.net/manual/en/function.abs.php to get your value positive
All the others are solvable by using http://php.net/manual/en/function.strtotime.php
In the worst case convert it to unix time and get the difference, you will have an accurate output without paying any attention to the leapyears and other time conditions which you have mentioned like the 28/29 feb problem.
Can I say that you don't want to do this? Store the regular Unix timestamp -- do the conversions to local timestamps if you need.
Data gets really hosed, really fast, when time zones change or someone moves daylight savings time around. Depending on how accurate you need your data, leap seconds (and even leap minutes) are possible.
The timehandling code is very complicated and /very/ smart.
I'm a big proponent of PHP's DateTime classes. They alleviate many of the frustrations that arise when dealing with time.
This sample illustrates how you might use it.
<?php
header('Content-type: text/plain');
$today = new DateTime("today");
echo 'Today: ' . $today->format('Y-m-d H:i:s') . "
";
echo 'Timestamp: ' . $today->getTimestamp() . "
";
$thisweek = new DateTime("this week");
echo 'This Week: ' . $thisweek->format('Y-m-d') . "
";
echo 'Timestamp: ' . $thisweek->getTimestamp() . "
";
$thismonth = new DateTime("this month");
echo 'This Month: ' . $thismonth->format('Y-m') . "
";
echo "Days this month: ", $thismonth->format('t') . "
";
echo 'Timestamp: ' . $thismonth->getTimestamp() . "
";
?>
You can enhance this by utilizing the DateTimeZone class.