Get future timestamp of date and month without knowing the year - php

I have a date string that contains the month and the day: "January 20"
I need to convert this to a timestamp in PHP and can use strtotime to do so. The catch is I always want to return a future timestamp. So if it's December 30, 2020 today, and I have the string "January 20," I want to return the timestamp for January 20, 2021 -- not January 20, 2020.
I've come up with a few convoluted ways of doing this, such as converting to a timestamp then making sure the timestamp is > the current time. If it's not, add a year and re-convert to a timestamp. But it seems like there may be a best practice and simple way of doing this. Any suggestions?
Thanks,
Tim

I'd try to compare current timestamp against this year timestamp. It should work.
$str = 'January 20';
$now = time();
$thisYear = strtotime($str);
$nextYear = strtotime($str . ' + 1 year');
$futureDate = $thisYear < $now ? $nextYear : $thisYear;
// 2021-01-20 - for January 20
// 2022-01-01 - for January 01

Related

Get Unix timestamp of specific date after another specific time

I can get the for example 19 March of specific date with this code:
$date = strtotime(" 19 March", $current_time);
For example if I gave the unix timestamp of 1st of January of 2010 as an input, It gave me 19 March of 2010. But also if I gave the unix timestamp of 20 March of 2010,I still get 19 March 2010. What I want is to get the next 19 March which in this case, It would be 19 March of 2011.
How can I do that?
Using PHP DateTime this can be achieved as follows:
// New DateTime object
$date = new DateTime('2010-03-19');
// Add a year
$date->add(new DateInterval('P1Y'));
// Output timestamp
echo $date->getTimestamp();
You can do something like as
$get = "19 March";
$given_date = "01 January 2010";
$date_month = date('d F',strtotime($given_date));
$year = date('Y',strtotime($given_date));
if(strtotime($given_date) - strtotime($date_month) < 0){
echo date('l,d F Y',strtotime("$get $year"));
}else{
echo date('l,d F Y',strtotime("$get ".($year+1)));
}
You should first get year from specified date. Then after you can create 19 march date with year and use strtotime() to get timestamp.
//add format according to your current_time variable format
$date = DateTime::createFromFormat("Y-m-d", $current_time);
echo $date->format("Y");
$fixed_date = strtotime($date->format("Y")."-03-19");
You can specify how many days or week you want to add or subtract from a day, as well as set the time with these functions
$nextUpdate = new DateTime("+5 day 1:00 pm");
echo $nextUpdate->getTimestamp();
$nextWeek = new DateTime("+1 week 9:00 am");
echo $nextWeek->getTimestamp();

PHP: get next date based on fixed base date

is there a way in PHP to get the next date(s) using a 4-week interval from a given date ?
Example:
My start date is Friday, Jan 03, 2014 and my interval is every 4 weeks from that date.
What I am looking for is the next date (or dates, if possible) from the current date that matches this 4-week interval.
In the above example this would be Friday, May 23, 2014 (then June 20, 2014, July 18, 2014 etc.).
I know I can get the current date as follows: $today = date('Y-m-d');
and I could probably set the start date like this: $start = date('2014-01-03');
but I don't know how to calculate the interval and how to find out the next matching date(s).
You should read up on the DateTime classes, specifically DatePeriod and DateInterval:
$start = new DateTime('2014-01-03');
$interval = DateInterval::createFromDateString('4 weeks');
$end = new DateTime('2015-12-31');
$occurrences = new DatePeriod($start, $interval, $end);
foreach ($occurrences as $occurrence) {
echo $occurrence->format('Y-m-d') . PHP_EOL;
}
DatePeriod takes a start date and a DateInterval and allows you traverse over the object to get all dates within the boundaries using the given interval. The cut off can be either a set number of cycles (so the next 10 dates) or an end date (like above), even if the end date is not one of the dates the interval falls on (it will stop below it). Or you can use an 8601 interval notation string (which sounds so much fun, huh?), but I'm pretty shaky on that.
If 4-week interval means 7 x 4 = 28 days, you can obtain the "next date" by:
$today = new DateTime();
$next_date = $today->add(new DateInterval('P28D'));
$next_next_date = $next_date->add(new DateInterval('P28D'));
$next_next_next_date = $next_next_date->add(new DateInterval('P28D'));
And if you want to calculate more "next dates", you can repeat the add() to repetitively add 28 days to your date.
Note: Beside using P28D, you can use P4W, which means 4 weeks.
While some answers may suggest using strtotime(), I find the object-oriented approach more structured. However, DateInterval is only available after PHP >= 5.3.0 (while DateTime is available after PHP >= 5.2.0)
You could use strtotime()
echo date('Y-m-d', strtotime('now +4 weeks'));
UPDATED:
$start = date('Y-m-d', strtotime('2014-01-03 +4 weeks'));
echo $start;
You could also run this in a for loop to get the next 6 or more dates. For example:
$Date = "2014-01-03";
$Int = 6;
for($i=0; $i<$Int; $i++){
$Date = date('Y-m-d', strtotime('{$Date} +4 weeks'));
echo $Date;
}

Get a dynamic timestamp in php?

How would you calculate an unix timestamp of the January 1st of the current year in PHP?
What I mean by current year is that I don't want to put 2012 in it because it should be a dynamic date, next year it should be 2013 and so on.
You can use :
mktime(0, 0, 0, 1, 1, date('Y'));
You can do this:
echo strtotime('01 january ' . date('Y'));
You mean you have a date and time like this 2012-01-01 06:30:54 and you want to convert them to unix timestamp?
If that's the case then strtotime() is the best way to go buddy.
e.g : strtotime("2012-01-01 06:30:54");
or
$datetime = "2012-01-01 06:30:54";
echo strtotime($datetime);

There is a simple way to get unix time range of a day if given a random timestamp from that day?

There is a simple way to get unix time range of a day if given a random timestamp from that day ?
I have a date like 1345547471 which is "Tue, 21 Aug 2012 11:11:11 GMT"
There is a php function that can receive a timestamp like this and return a 00:00 hours timestamp and a 23:59 hours timestamp of that day ?
Thank you.
Sure, DateTime can do that:
$time = 1345547471;
$date = new DateTime;
// $date->setTimezone( new DateTimeZone( "America/New_York")); // Can set TZ here if needed
$date->setTimestamp( $time);
Now, you can set the time to whatever you want:
$date->setTime( 0, 0, 0); // 0h 0m 0s
And grab the resulting UNIX Timestamp:
$timestamp = $date->getTimestamp();
Same thing for the next use-case:
$date->setTime( 23, 59, 0);
$timestamp = $date->getTimestamp();
It is important to note that DateTime will properly handle cases of daylight savings time and local time discontinuities.
You can use the mod (gives the remainder after a division) PHP function like this to get the first second of a Unix timestamp (ie, today 0:00:00)
$var=time()-(time()%86400);
Then with this unix timstamp, you can add 86399 to get the last second of the day.
Edit: This doesn't account for dalylight savings.
$ts = 1345547471;
$ts_00_00 = mktime(0,0,0, date("m", $ts), date("d",$ts), date("Y",$ts);
$ts_23_59 = mktime(23,59,59, date("m", $ts), date("d",$ts), date("Y",$ts);
Documentation:
http://php.net/manual/en/function.date.php
http://php.net/manual/en/function.mktime.php
If you are using PHP >= 5.3.0 Then you can use this...
Check out for this.
http://www.php.net/manual/en/datetime.createfromformat.php
This is similar to Fluffeh's answer, but accounts for daylight savings time. This is based on the server's time zone.
//Get time range for today
$start = strtotime(date("Y-m-d")." 00:00:00");
$end = strtotime(date("Y-m-d")." 23:59:59");
//Show our date in a human-readable format for troubleshooting
echo date(DATE_RFC1036,$start)."<br>".date(DATE_RFC1036,$end);
If you want to specify a custom timezone instead of the server timezine, you can add it to like so:
//Get time range for today
$start = strtotime(date("Y-m-d")." 00:00:00 -0500");
$end = strtotime(date("Y-m-d")." 23:59:59 -0500");
//Show our date in a human-readable format for troubleshooting
echo date(DATE_RFC1036,$start)."<br>".date(DATE_RFC1036,$end);
Link to working Sample

strtotime - a second before midnight

I attempted this:
$date_string = strtotime('6 Mar, 2011 23:59:59');
But I think PHP can't interpret that for some reason as it returned empty. I tried this:
$date_string = strtotime('6 Mar, 2011 midnight');
The above worked but I need it to be a second before midnight i.e. the last second of the day. How can I get strtotime to return this without changing the 6 Mar, 2011 part?
Hope this helps. I used this and it gives todays timestamp just before midnight. Counter intuitive.
$today_timestamp = strtotime('tomorrow - 1 second');
It works for me if I use March 6, 2011 23:59:59. Any chance of changing the input format?
Other than that, you could of course subtract 1 second from the timestamp. Note however that you need to use March 7:
$date_string = strtotime('7 Mar, 2011 midnight') - 1;
Why not use mktime?
mktime(23,59,59,3,6,2011);
If you're on PHP 5.3 or greater, you could use the DateTime class.
The createFromFormat function allows you to manually specify how to parse your input date string.
$date = '6 Mar, 2011 23:59:59';
$timestamp = DateTime::createFromFormat('d M, Y H:i:s', $date)->getTimestamp();

Categories