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)
Related
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 get last year's start and end date using PHP code? Is it possible?
The first day is always January 1, the last day is always December 31. You're really only changing the year attached to it. Depending on how you want the date formatted, you have a couple possibilities...
If you just want to display the physical date:
$year = date('Y') - 1; // Get current year and subtract 1
$start = "January 1st, {$year}";
$end = "December 31st, {$year}";
If you need the timestamp for both those dates:
$year = date('Y') - 1; // Get current year and subtract 1
$start = mktime(0, 0, 0, 1, 1, $year);
$end = mktime(0, 0, 0, 12, 31, $year);
Very simple stuff. You can manually specify which year if you wanted too. The premise is the same.
You can do it by using the below. Hope it helps someone.
//to get start date of previous year
echo date("d-m-y",strtotime("last year January 1st"));
//to get end date of previous year
echo date("d-m-y",strtotime("last year December 31st"));
start date of the year :
mktime(0,0,0,1,1,$year);
end date of the year :
mktime(0,0,0,1,0,$year+1);
Check this Stuff
$currentY = date('Y');
$lastyearS = mktime(0, 0, 0, 1, 1, $currentY-1 )."<br/>";
$lastyearE = mktime(0, 0, 0, 12, 31, $currentY-1 )."<br/>";
echo date('Y-m-d',$lastyearS)."<br/>";echo date('Y-m-d',$lastyearE);
Suppose if your current month is February or the month which has 30 days
echo date('Y-12-t', strtotime(date('Y-m-d'))); // if current month is february (2015-02-01) than it gives 2015-02-28
will give you inaccurate results
Solution:
So to get accurate result for the end date of an year, try the code below
$start_date = date("Y-01-01", strtotime("-1 year"));// get start date from here
$end_date = date("Y-12-t", strtotime($start_date));
(OR)
$last_year_last_month_date = date("Y-12-01", strtotime("-1 year"));
$end_date = date("Y-12-t", strtotime($last_year_last_month_date));
I found this script on php.net and finds the difference between now and a future day. My question is very simple, but is a sample time or how can I make that time that it is needed for the $future_date ? Also what is the purpose of -1970 ?
Also how can I show a message when the future_date is reached or passed?
<?php
function time_difference($endtime){
$days= (date("j",$endtime)-1);
$months =(date("n",$endtime)-1);
$years =(date("Y",$endtime)-1970);
$hours =date("G",$endtime);
$mins =date("i",$endtime);
$secs =date("s",$endtime);
$diff="'day': ".$days.",'month': ".$months.",'year': ".$years.",'hour': ".$hours.",'min': ".$mins.",'sec': ".$secs;
return $diff;
}
$end_time = $future_date - time();
$difference = time_difference($end_time);
echo $difference;
//sample output
'day': 2,'month': 1,'year': 0,'hour': 2,'min': 05,'sec': 41
?>
A unix timestamp checkout the docs for time() and mktime()
You're substracting two values from each other so they need to be compatibable formats to be able to do that. Checking the documentation on time() could have saved you from this question.
date() is also a function you might want to check up on. Using date and the right parameters it will return the current year(Y) month(m) or day of the month(d) you can add and substract to these values and then pass them into mktime to get a unix timestamp like so for the current year in unix timestamp format:
$currentyear = mktime(date(Y));
Below would set $future_date to 1st Dec 2011
$future_date = mktime(0, 0, 0, 12, 1, 2011);
So Hour Min Sec goes:
$future_date = mktime(H, M, S, 12, 1, 2011);
Below would be 13:21:59 1st Dec 2011
$future_date = mktime(13, 21, 59, 12, 1, 2011);
$future_date should be a unix timestamp.
$future_date = strtotime("next week");
To check if the time has been reached
if($future_date <= time()) echo "Date reached";
$future_date would be an integer timestamp (in seconds since Jan 1, 1970) representing some time in the future.
ie:
$nextWeek = time() + (7 * 24 * 60 * 60);
Takes the current date/time and adds 7 days worth of seconds (24 hours, 60 minutes per hour, 60 seconds per minute) to get the integer time of one week from now.
Jan 1, 1970 is significant - it is called the Epoch in UNIX (January 1 1970 00:00:00 GMT) and is often used as a starting point for dates and/or computer "time" (time zero).
I would like to set a cookie with PHP that has to expire at the end of the month.
How can I get the number of seconds until the end of the month?
Thank you.
You can use time() to get the number of seconds elapsed since the epoche. Then use strtotime("date") to get the number of seconds to your date. Subtract the two and you have the number of seconds difference.
This will give you the last second of the month:
$end = strtotime('+1 month',strtotime(date('m').'/01/'.date('Y').' 00:00:00')) - 1;
This will give you now:
$now = time();
This will give you the distance:
$numSecondsUntilEnd = $end - $now;
If you're using setcookie() function, then you don't really need the number of seconds, you need the timestamp, when cookie should be expired:
// Works in PHP 5.3+
setcookie("cookie_name", "value", strtotime("first day of next month 0:00"));
// Example without using strtotime(), works in all PHP versions
setcookie("cookie_name", "value", mktime(0, 0, 0, date('n') + 1, 1, date('Y')));
In PHP 5.3 they added a DateTime class which makes handling operations like this make a lot more sense and a little bit easier too (in my opinion).
$datetime1 = new DateTime('now'); // current date
$datetime2 = new DateTime(date("Ymt")); // last day in the month
$interval = $datetime1->diff($datetime2); // difference
echo $interval->format('%d') * 86400; // number of seconds
Create a timestamp for the end of the month and subtract the timestamp for the current time from it.
// Create a timestamp for the last day of current month
// by creating a date for the 0th day of next month
$eom = mktime(0, 0, 0, date('m', time()) + 1, 0);
// Subtract current time for difference
$diff = $eom - time();
What is the simplest way to do it in PHP ?
I want the date of the Monday of a given week number of a year (example : week number 3 of 2009)
Thanks !
EDIT : If you use Linux only machines, use cletus' solution, however I am looking for something that can work on Windows AND Linux.
It's simple on PHP 5.3
echo date('M d',strtotime('2013W15'));
where 15 is the number of week. But for the number below ten make sure it is in the format of 01, 02 for first week and second week.
Yet another solution:
<?php
$week = 3;
$year = 2009;
$timestamp = mktime( 0, 0, 0, 1, 1, $year ) + ( $week * 7 * 24 * 60 * 60 );
$timestamp_for_monday = $timestamp - 86400 * ( date( 'N', $timestamp ) - 1 );
$date_for_monday = date( 'Y-m-d', $timestamp_for_monday );
?>
A nice way to get this in a clean way is by using php DateTime class.
$year = 2015;
$week_no = 1;
$date = new DateTime();
$date->setISODate($year,$week_no);
echo $date->format('d-M-Y');
This would result into : 29-12-2014
You can use strptime() to get the time.
$time = strptime('1 23 2009', '%w %U %Y');
This will get the time for the Monday (day 1, 0 is Sunday, 6 is Saturday) of the 23rd week of 2009. If you want to format this into a date, use date().
$date = date('d F Y', $time);
This next script gives the 7 days of an specific week of a year
$time = new DateTime();
$time->setISODate(2016, 13);
for($i=0;$i<7;$i++){
echo $time->format('d-M-Y') . '<br />';
$time->add(new DateInterval('P1D'));
}
Seems to be working and not dependent of the server OS :
<?php
$week = 4;
$year = 2013;
$timestamp_for_monday = mktime( 0, 0, 0, 1, 1, $year ) + ((7+1-(date( 'N', mktime( 0, 0, 0, 1, 1, $year ) )))*86400) + ($week-2)*7*86400 + 1 ;
?>
the idea is to add :
the timestamp of the first of January of the chosen year
the number of seconds to reach the end of the first week (which is 7 days minus the day of week of the 1st of January + 1 day) multiplied by the number of seconds per day
the number of seconds of the number of chosen weeks minus the first week and the current week
1 second to reach the fist second of the current week
My example returns : 1358722801
which is the timestamp of 2013/01/21 0:00:01
Here is very simple solution, passing a week no and returns the date.
The ISO8601 standard states that week 1 always fall on the week where Jan 4 falls.
For example, to get a day in the 4th week of the year:
$day_in_week = strtotime("2006-01-04 + 4 weeks"));
Then you can adjust this value to Sunday (as a starting place you can guarantee that you can find):
// Find that day's day of the week (value of 0-6)
$wday = date('w', $day_in_week);
$offset = 6 - $wday; // How far it is from Sunday.
$sunday_in_week = $day_in_week - ($offset * (60 * 60 * 24)); // $offset * seconds in a day
Then, you add the seconds in a day again to get Monday.
$monday_in_week = $sunday_in_week + (60 * 60 * 24);
Note: This method can occasionally have some problems with daylight savings time. A similar, and slightly safer between DST time changes, method would use the DateTime class. However, DateTime is only support in PHP 5.2.0 or later. The method above works in earlier version as well.
Try this function
function MondayOfWeek($WeekNumber, $Year=-1) {
if ($Year == -1) $Year = 0+date("Y");
$NewYearDate = mktime(0,0,0,1,1,$Year);
$FirstMondayDate = 7 + 1 - date("w", mktime(0,0,0,1,1,2009));
$Dates_fromFirstMonday = 7 * $WeekNumber;
$Second_fromFirstMonday = 60*60*24*($FirstMondayDate + $Dates_fromFirstMonday);
$MondayDay_ofWeek = $NewYearDate + $Second_fromFirstMonday;
$Date_ofMondayDay_ofWeek = 0+date("j", $MondayDay_ofWeek);
return $Date_ofMondayDay_ofWeek;
}
for($i = 0; $i
When run, I got:
-5-12-19-26-2-9-16-23-2-9-16-23-30-6-13-20-27-4-11-18-25-1-8-15-22-29-6-13-20-27-3-10-17-24-31-7-14-21-28-5-12-19-26-2-9-16-23-30-7-14-21-28
Hope this helps.
I required same in the java script..so i converted.
function(week,year){
var timestamp = new Date(year, 0, 1, 0, 0, 0, 0);
var dateObj=new Date();
var val = timestamp.getTime();
days=( week * 7 * 24 * 60 * 60*1000 );
val=val+days;
var timestamp_for_monday =val - 86400 *((timestamp.getDay()-1000));
var weekdate=new Date(timestamp_for_monday);
return weekdate;
}