strtotime

PHP date() and strtotime() return wrong months on 31st

倖福魔咒の 提交于 2019-11-26 18:56:52
I'm using date() and strtotime() functions to display the next 3 months in a dropdown list. PHP Code: echo date("m/Y",strtotime("+0 months")); echo date("m/Y",strtotime("+1 months")); echo date("m/Y",strtotime("+2 months")); However, if the script is running when the server date is on 30th or 31st, the next month, which is Feburary, will be displayed as March instead. i.e. the script above is supposed to return 01/2012 02/2012 03/2012 But, instead of that, it actually displays 01/2012 03/2012 03/2012 that is because Feburary doesn't have 30th or 31st, so the script translates "31/02" into "01

Adding 30 minutes to time formatted as H:i in PHP

徘徊边缘 提交于 2019-11-26 18:48:35
Having a nightmare at the moment and just can't see why it isn't working I have a value in the form H:i (ie 10:00, 13:30) etc called $time What I want to do is create two new values, $startTime which is 30 mins before $time and $endTime which is 30 mins after $time I have tried the following but just doesn't seem to want to work $startTime = date("H:i",strtotime('-30 minutes',$time)); $endTime = date("H:i",strtotime('+30 minutes',$time)); If I pass through 10:00 as $time and echo out both $startTime and $endTime I get: $startTime = 00:30 $startTime = 01:30 $time = strtotime('10:00');

Convert this string to timestamp PHP [duplicate]

筅森魡賤 提交于 2019-11-26 18:24:04
问题 This question already has answers here : Convert one date format into another in PHP (15 answers) Closed 6 years ago . I have this string: "13/10 15:00" and I would like to convert it to timestamp but when I do this: $timestamp = strtotime("13/10 15:00"); It returns an empty value. 回答1: In your code strtotime() is attempting to convert 13/10 as the tenth day of the 13th month, which returns an error. If you want to parse a date string with a custom format, it's better to use DateTime:

strtotime With Different Languages?

夙愿已清 提交于 2019-11-26 17:55:18
Does strtotime only work in the default language on the server? The below code should resolve to august 11, 2005, however it uses the french "aout" instead of the english "aug". Any ideas how to handle this? <?php $date = strtotime('11 aout 05'); echo date('d M Y',$date); ?> From the docs Parse about any English textual datetime description into a Unix timestamp Edit: Six years down the road now, and what was meant to be a side-note about why strtotime() was an inappropriate solution for the issue at hand became the accepted answer 😲 To better answer the actual question I want to echo Marc B's

PHP, Get tomorrows date from date

时光总嘲笑我的痴心妄想 提交于 2019-11-26 17:28:12
问题 I have a PHP date in the form of 2013-01-22 and I want to get tomorrows date in the same format, so for example 2013-01-23 . How is this possible with PHP? 回答1: Use DateTime $datetime = new DateTime('tomorrow'); echo $datetime->format('Y-m-d H:i:s'); Or: $datetime = new DateTime('2013-01-22'); $datetime->modify('+1 day'); echo $datetime->format('Y-m-d H:i:s'); Or: $datetime = new DateTime('2013-01-22'); $datetime->add(new DateInterval("P1D")); echo $datetime->format('Y-m-d H:i:s'); Or in PHP

获取最近7天,7周,7月

喜你入骨 提交于 2019-11-26 16:55:54
/** * 获取最近七天 * @param string $date * @param string $format * @return array */ public static function getSevenDay($date = '', $format = 'Y-m-d') { $time = $date ? strtotime($date) : time(); //组合数据 $out_data = []; for ($i = 1; $i <= 7; $i++) { $out_data[$i - 1] = date($format, strtotime('+' . $i - 7 . ' days', $time)); } return $out_data; } /** * 获取最近七月的日期,大于等于1 * @param string $date * @return array */ public static function getLastSevenMonth($date = '') { $result = []; $month = $date ? date('m',strtotime($date)) : date('m'); $year = $date ? date('Y',strtotime($date)) : date('Y'); for($i=0;$i<7;

Working days (Mon-Fri) in PHP

两盒软妹~` 提交于 2019-11-26 16:20:23
问题 Is there a way to use strtotime to add working days (Monday to Friday) to a date? Or some other method? What I want to do is: date ( 'Y-m-j' , strtotime ( '+3 working days' ) ) 回答1: If you are limiting to weekdays use the string weekdays. echo date ( 'Y-m-j' , strtotime ( '3 weekdays' ) ); This should jump you ahead by 3 weekdays, so if it is Thursday it will add the additional weekend time. Source: http://www.php.net/manual/en/datetime.formats.relative.php 回答2: I have found this buggy when

PHP date conversion to strtotime

跟風遠走 提交于 2019-11-26 14:58:43
问题 I have an array where each record has a 'date' field. The dates are in this format: '10/09/2015' . When I use strtotime on these dates, some of them come out as false . I thought there was a mistake in data structure so I created this simple array and a simple foreach loop, and they output false in some cases. What does it not like? $dates = []; $someData = [ '21/05/2014', '22/05/2014', '09/06/2014', '04/07/2014', ]; foreach($someData as $date) { $dates[] = strtotime($date); } print_r($dates)

DateTime class vs. native PHP date-functions

折月煮酒 提交于 2019-11-26 14:41:16
问题 The DateTime class sure has some handy methods and seems overall superior to the native PHP date functions like strtotime , mktime and strftime (and more). But is there any drawback or a reason why I shouldn't use it ? The only reason I can think of is that it might be more expensive to create a whole instance of a class than just using a function. Would you agree with that ? Does it make sense at all to use a DateTime object for simple stuff? Are there any other drawbacks ? It seems a bit

php - add two hours to date variable

混江龙づ霸主 提交于 2019-11-26 14:39:20
问题 I want to add 3 minutes to a date/time variable I have, but I'm not sure how to do this. I made the variable from a string like this: (which is in the RFC 2822 date format btw) $date = 2011-10-18T19:56:00+0200 I converted that string into date using this command: $time = date_format(DateTime::createFromFormat("Y-m-d\TH:i:sO", $date), "G:i") Now, I'd like to add 3 minutes to that variable, but I I'm not sure how. I've used the following command in my script before, but that applies to the