strtotime

Check if current time is between two times, with the possibility of lapping days

╄→尐↘猪︶ㄣ 提交于 2019-12-03 15:54:51
I have a system that accepts user submissions, and upon receiving a submission the system will go through all timeslots to find the appropriate timeslot. The problem is that it needs to be able to check against the start & end times if the end time laps to the next day. Take the following example: A timeslot begins at 10:30 PM on the current day and ends at 4:00 PM the next day. If the current time is between 10:30 PM and 11:59:59 PM, the submission will be assigned to that timeslot. However, if the current time is between 12:00 AM and 4:00 PM then it will skip the timeslot. This is what I

PHP - Strtotime - Add hours

谁说我不能喝 提交于 2019-12-03 12:28:31
I have this variable: $timestamp = strftime("%Y-%m-%d %h:%M:%S %a", time ()); I simply want to add three hours and echo it out. I have seen the way where you can do the 60 * 60 * 3 method or the hard code "+ 3 hours" where it understands the words. What is the best way of getting this result? $timestamp = strftime("%Y-%m-%d %h:%M:%S %a", time() + 3*60*60) 3*60*60 is the best way The best way is what you think is more readable. The following expressions are identical: time() + 3 * 60 * 60 strtotime('+3 hours') i always do like this $current_time = date('Y-m-d H:i:s'); $new_time = strtotime(

Adding days to specific day

只谈情不闲聊 提交于 2019-12-03 10:40:55
Many examples are about adding days to this day. But how to do it, if I have different starding day? For example (Does not work): $day='2010-01-23'; // add 7 days to the date above $NewDate= Date('$day', strtotime("+7 days")); echo $NewDate; Example above does not work. How should I change the starding day by putting something else in the place of Date? For a very basic fix based on your code: $day='2010-01-23'; // add 7 days to the date above $NewDate = date('Y-m-d', strtotime($day . " +7 days")); echo $NewDate; If you are using PHP 5.3+, you can use the new DateTime libs which are very handy

Next month, same day in PHP

匿名 (未验证) 提交于 2019-12-03 08:36:05
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I have an "event" that needs to be scheduled the same day of every month. Say you set the start date on the 1st May you should get the next events on the 1st of Jun, 1 Jul etc. The problem comes with a start date on the 31st (the next ones could be 30 or 28 depending on the month). Considering that there are months with different numbers of days (28, 30, 31) depending on the month itself and the year... what would be an easy way to setup this? Consider the following (and flawed) nextmonth function: $events = array() function nextmonth($date)

php strtotime reverse

北城以北 提交于 2019-12-03 08:30:28
Is there some function timetostr in php that will output today/tomorrow/next sunday/etc. from a given timestamp? So that timetostr(strtotime(x))=x This might be useful for people coming here. /** * Format a timestamp to display its age (5 days ago, in 3 days, etc.). * * @param int $timestamp * @param int $now * @return string */ function timetostr($timestamp, $now = null) { $age = ($now ?: time()) - $timestamp; $future = ($age < 0); $age = abs($age); $age = (int)($age / 60); // minutes ago if ($age == 0) return $future ? "momentarily" : "just now"; $scales = [ ["minute", "minutes", 60], ["hour

PHP strtotime() function that accepts a format?

大憨熊 提交于 2019-12-03 08:10:34
strtotime() in PHP works great if you can provide it with a date format it understands and can convert, but for example you give it a UK date it fails to give the correct unix timestamp. Is there any PHP function, official or unofficial, that can accept a format variable that tells the function in which format the date and time is being passed? The closest I have come to doing this is a mixture of date_parse_from_format() and mktime() // Example usage of the function I'm after //Like the date() function but in reverse $timestamp = strtotimeformat("03/05/2011 16:33:00", "d/m/Y H:i:s"); If you

Return current date plus 7 days

匿名 (未验证) 提交于 2019-12-03 02:47:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 由 翻译 强力驱动 问题: I'm Trying to get the current date plus 7 days to display. Example: Today is August 16, 2012, so this php snippet would output August 23, 2012. $date = strtotime ( $date ); $date = strtotime ( "+7 day" , $date ); echo date ( 'M d, Y' , $date ); Right now, I'm getting: Jan 08, 1970. What am I missing? 回答1: strtotime will automatically use the current unix timestamp to base your string annotation off of. Just do: $date = strtotime ( "+7 day" ); echo date ( 'M d, Y' , $date ); Added Info For Future Visitors: If you need to pass a

Get all week start date and end date within a date range in php

匿名 (未验证) 提交于 2019-12-03 02:41:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 由 翻译 强力驱动 问题: How to get all week start date and end date within a date range in PHP? Week start = Sunday and week end = Saturday Input $start_date='2013-02-01' $end_date = '2013-02-28' Output start date='2013-02-01' End date ='2013-02-02' start date='2013-02-03' End date ='2013-02-09' start date='2013-02-10' End date ='2013-02-16' start date='2013-02-17' End date ='2013-02-23' start date='2013-02-24' End date ='2013-02-28' Below code return the week start and end of the date given function getWeekDates ( $date ) { $week = date ( 'W' , strtotime

PHP strtotime returning false for UTC time

匿名 (未验证) 提交于 2019-12-03 02:38:01
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: My colleague and I are obtaining different results from some unit tests that use strtotime. The discrepancy originates in this line: $value = strtotime('2050-05-01T20:10:29.410Z'); on my machine, this result returns the following: int(2535048629) whereas my colleague's version returns false We are both using PHP version 5.4.14 and PHPUnit 3.724. Has anyone got any idea what is causing this discrepancy, and is there a more robust approach? 回答1: This is because he is on 32-bit and you are on 64-bit machine. See what echo PHP_INT_MAX; returns

Rounding a MYSQL datetime to earliest 15 minute interval in milliseconds (PHP)

匿名 (未验证) 提交于 2019-12-03 02:29:01
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I'm fetching a datetime from MYSQL which looks like: 2010-08-11 11:18:28 I need to convert it into the "floor" or the earliest 15 minute interval and output in milliseconds for another function. So, this case would be: 2010-08-11 11:15:00 in milliseconds Whoops! Sorry - need to clarify - I need code that will transform it into milliseconds WITHIN php! Doing a timing test revealed the following: $time_start = microtime(true); for($i=0;$i<10000;$i++) floor(strtotime('2010-08-11 23:59:59')/(60*15))*60*15*1000; $time_end = microtime(true); echo