How many seconds until my PHP mktime future date - php

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();

Related

Generating random unix timestamp for tomorrow's date

I'm trying to generate a random unix timestamp for the following day in PHP. Could anyone point me into the right direction as to how this could be done?
Thanks!
Frank
If you mean a random timestamp between 12:00am and 11:59pm you can do:
$tomorrow0000 = mktime(0, 0, 0, date('n'), date('d')+1, date('Y')); // midnight tomorrow
$tomorrow2359 = mktime(0, 0, 0, date('n'), date('d')+2, date('Y')) - 1; // midnight next day minus 1 second
$random = mt_rand($tomorrow0000, $tomorrow2359);
I think this could work:
$tomorrow_time = strtotime("tomorrow") + rand(0, 86400);
Basically, I get tomorrow midnight time and then add random second from 0 to 86400 (24 hours)

Unix Timestamp calculate date with DST in mind

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

PHP Unix timestamp

I have a code but can't find where is an error. I have upcoming event and current time. If I convert my event to the timestamp it's less then current timestamp. May be you can help me.
My code below:
<?php
date_default_timezone_set('Etc/GMT');
$upcoming = "2012.09.05 23:50";
$current = time();
echo "Upcoming: " . $upcoming . " | Timestamp:" . mktime(23, 50, 0, 09, 05, intval(date("Y")));
echo "<br>Current: " . time();
echo "<br>Current SIM: " . mktime(date("H"), date("i"), date("s"), date("m"), date("d"), date("Y"));
?>
Will output:
Upcoming: 2012.09.05 23:50 | Timestamp:1323129000
Current: 1346855221
Current SIM: 1346855220
Where Current > Upcoming timestamp. (???)
Thanks!
Because you have 09 (with a preceding 0) this number is interpreted as an octal number, and so it's converted to 0.
Use: mktime(23, 50, 0, 9, 5, intval(date("Y")));
You can explore this "feature" a bit;
var_dump(9); // int 9
var_dump(09); // int 0
var_dump(07); // int 7
var_dump(17); // int 17
var_dump(017); // int 15
EDIT;
date('n'); returns the month without leading zeros. And date('j'); and date('G'); return the day and hour without leading zeros. So you can change mktime(date("H"), date("i"), date("s"), date("m"), date("d"), date("Y")); to mktime(date("G"), date("i"), date("s"), date("n"), date("j"), date("Y"));
There's no way to get the number of minutes and seconds without leading zeros with date() so maybe you need to find another function for that.
EDIT:
To convert 2012.09.05 23:50 to a timestamp you can change the . to / and feed it to strtotime():
$str = '2012.09.05 23:50';
$str = str_replace(".", "/", $str);
$timestamp = strtotime($str);
Leave away the unnecessary zeroes, that should do the trick.
mktime(23, 50, 0, 9, 5, intval(date("Y")));

Get current date and time form mktime in PHP

I have a problem with find the current date from past mktime. In PHP I find the current date using date("j");. Here I need, suppose my date was in the past year like mktime(0, 0, 0, 2, 1, 2008), then here how can I find the current date of this particular past month.
Either as #octern's solution, or you can do
$day = date('j', strtotime("-2 months"));
or
$day = date('j', strtotime('-30 days'));
depending on your need.
You may also want to refer to strtotime() manual.
Try this:
$date = getdate(mktime(0, 0, 0, 2, 1, 2008));
$day = $date['mday'];
Or just:
$day = date('j', mktime(0, 0, 0, 2, 1, 2008))

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);

Categories