Unix Timestamp calculate date with DST in mind - php

How do I calculate the unix timestamp of yesterday where we keep the dst in mind?
Normally I would do $timestamp - 86400 but that does not work when yesterday was without DST and today is.

You should use the DateTime class and you'll have to specifiy the Timezone of England (BST) in the Timestring. Like this:
$dt = new DateTime('Yesterday BST');
$timestamp = $dt->getTimestamp(); // first second of 'yesterday'
The example above will respect daylight saving times.

How about:
$startTime = mktime(0, 0, 0, date('m'), date('d')-1, date('Y'));
$endTime = mktime(23, 59, 59, date('m'), date('d')-1, date('Y'));
This is from:
http://en.kioskea.net/faq/1861-mktime-timestamp-yesterday-last-month-etc

Related

How many seconds until my PHP mktime future date

So I have my code that fetches the date of the first of the next month at midnight:
$future_date = date("Y-m-d H:i:s", mktime(0, 0, 0, date("m")+1, 1, date("Y")));
What I can't figure out (and I've Googled a fair bit) is how to count the seconds from NOW until that future date.
Storing mktime() into $future_date_unix means you can use it for both the string generation you require and the seconds calculation using a subtraction of the current time() value.
$future_date_unix = mktime( 0, 0, 0, date('m') + 1, 1, date('Y') );
$future_date = date( 'Y-m-d H:i:s', $future_date_unix );
$seconds_till_future_date = $future_date_unix - time();

How can i convert date("j/n/y") to epoch

I have some data that makes use of date("j/n/y") format i.e date for today is 23/1/15
I have tried
echo strtotime($today);
but this does not give me the timestamp i want.How would i convert a date in date("j/n/y") format to epoch?.
Use DateTime::createFromFormat() to read the date format and then use DateTime::getTimestamp() to format it as a timestamp.
$date = DateTime::createFromFormat('j/n/y', '23/1/15');
$epoch = $date->getTimestamp();
I think you're looking for the mktime function in PHP. It goes a little like this:
$timestamp = mktime(0,0,0,0,0,0);
Where, in order, the arguments are: hour, minute, second, month, day, year. So, in your case:
$today = mktime(0, 0, 0, 1, 23, 2015);
// Would return the timestamp for Jan. 23rd, 2015 at 12:00:00 am (I think)
If you're looking for a dynamic right now timestamp, you may use date() in each of the arguments of mktime. For example:
$rightnow = mktime(date("H"), date("i"), date("s"), date("m"), date("d"), date("Y"));
// Would return the timestamp for Jan. 23rd, 2015 at 10:57:25 am.
But, as John Conde says, it requires you break apart the date before you can use it, so it may not be as efficient.
Hope that helps!
Just to have another approach this one would be good for 85 more years.
$date = date('j/n/y', time());
list($day, $month, $year) = explode("/", $date);
$date = "20" . $year . "-" . $month . "-" . $day;
echo date('m/d/Y', strtotime($date));

How to set an specific date?

I'm trying to create an date with
$date_end = mktime(0, 0, 0, date('m'), date('d')+7, date('Y'), $date_set);
The output is today + 7 days instead of the date given + 7.
The manual says nothing about mktime() taking a date as argument.
Use strtotime("+7 days", $date_set).
$date_end = mktime(0, 0, 0, date('m', $date_set), date('d', $date_set)+7, date('Y', $date_set));
is, I believe, what you were trying to accomplish (assuming $date_set is a timestamp). Else, #Kristian's suggestion I believe is a good one.
Why are you passing a $date_set variable, and why are you using mktime if you already have the time?
Simply add 7 days: $date_end = $date_set + (7 * 86400);

Is it possible to create a date string, from a timestamp and a timezone offset?

All the Date/Time extension is based on timezone strings such as "Europe/Amsterdam".
The dropdown I show to the user has one option per timezone offset, eg: "(UTC -3:00) Argentina, Brazil, French Guiana, Uruguay"
I don't think showing him a list with all the countries in the world would be appropiate, since the only thing that matters is the timezone offset.
Is it possible to create a localized date object, or date string, from something like '-2' (as the timezone offset) ??
Answer: date('Y-m-d h:i:s', $unix_timestamp + $offset * 60 * 60)
You can feed date function the second argument using mktime .
Smth like this :
var_dump($endDate = date('Y-m-d h:i:s', mktime(0-2, 0, 0, 2, 1, 2011)));
Or using the datetime class like this :
$date = new DateTime('2000-01-01', new DateTimeZone('Pacific/Nauru'));
echo $date->format('Y-m-d H:i:sP') . "\n";
date_default_timezone_set('Europe/Bucharest');
# equivalent of GMT since Europe/Bucharest is +2 , we feed -2 for the hour
var_dump($endDate = date('Y-m-d h:i:s', mktime(date('h')-2, date('i'), date('s'), date('d'), date('m'), date('Y'))));
# equivalent of GMT+1 since Europe/Bucharest is +2 we feed -1 for the hour
var_dump($endDate = date('Y-m-d h:i:s', mktime(date('h')-1, date('i'), date('s'), date('d'), date('m'), date('Y'))));
you can allso feed +3 for the hour , taking Europe/Bucharest as the default timezone +3 would give the GMT +5 , you can allso set the default timezone Europe/London witch is equivalent of GMT0 then the user sends you +2 you will feed mktime with +2 for the hour witch will give you the equivalent of GMT+2 europe/bucharest .
date('Y-m-d h:i:s', $unix_timestamp + $offset * 60 * 60)

php date functions

I've rarely touched PHP date functions,
and now I need to do the follows:
get current date,
get date of three days later
get date of three weeks later
get date of three month later
get date of three years later
and finally to implement such a function:
function dage_generate($number,$unit)
{
}
$unit can be day/week/month/years
http://uk.php.net/strtotime can do most of that:
strtotime("today")
strtotime("+ 3 days")
strtotime("+ 3 weeks")
strtotime("+ 3 months")
strtotime("+ 3 years")
The function would be something like:
function dage_generate($number,$unit)
{
return strtotime("+ ".$number." ".$unit);
}
http://us.php.net/manual/en/function.date.php
Note towards the bottom of the page:
Example #3 date() and mktime() example
<?php
$tomorrow = mktime(0, 0, 0, date("m") , date("d")+1, date("Y"));
$lastmonth = mktime(0, 0, 0, date("m")-1, date("d"), date("Y"));
$nextyear = mktime(0, 0, 0, date("m"), date("d"), date("Y")+1);
?>
Using strtotime() you can do this easily.
$now = time();
$threeDays = strtotime("+3 days");
$threeWeeks = strtotime("+3 weeks");
$threeMonths = strtotime("+3 months");
$threeYears = strtotime("+3 years");
Each of those variables will be an integer which represents the unix timestamp at that point in time. You can then format it into a human readable string using date().
echo date('r', $threeWeeks);
// etc...
Use date_create() to create a DateTime object, then use the add method.
See the examples on the page for the add() method, they contain all you need.
PHP Date/Time Functions with examples

Categories