strtotime

php function for get all mondays within date range

最后都变了- 提交于 2019-11-27 07:41:01
Example: $startDate is Monday 2007-02-05 and $endDate is Tuesday 2007-02-20. Then I want it to list: Monday 2007-02-05 Monday 2007-02-12 Monday 2007-02-19 I looked at the PHP manual and found this to get all the days between two dates. But how to do it the way i want? PHP Code: Rather than get all days and loop through them all, get the first Monday after the start date and then iterate 7 days at a time: $endDate = strtotime($endDate); for($i = strtotime('Monday', strtotime($startDate)); $i <= $endDate; $i = strtotime('+1 week', $i)) echo date('l Y-m-d', $i); I needed the same and created a

How to convert week number and year into unix timestamp?

♀尐吖头ヾ 提交于 2019-11-27 06:33:49
问题 I'm trying to group together dates into a week number and year, and then I want to convert that week number back into a unix timestamp. How can I go about doing this? 回答1: I assume you are using ISO 8601 week numbers, and want the first day of a ISO 8601 week so that e.g. Week 1 of 2011 returns January 3 2011 . strtotime can do this out of the box using the {YYYY}W{WW} format: echo date("Y-m-d", strtotime("2011W01")); // 2011-01-03 Note that the week number needs to be two digits. Shamefully,

PHP strtotime +1 month behaviour

﹥>﹥吖頭↗ 提交于 2019-11-27 04:48:50
I know about the unwanted behaviour of PHP's function strtotime For example, when adding a month (+1 month) to dates like: 31.01.2011 -> 03.03.2011 I know it's not officially a PHP bug , and that this solution has some arguments behind it, but at least for me, this behavior has caused a lot waste of time (in the past and present) and I personally hate it. What I found even stranger is that for example in: MySQL: DATE_ADD('2011-01-31', INTERVAL 1 MONTH) returns 2011-02-28 or C# where new DateTime(2011, 01, 31).AddMonths(1); will return 28.02.2011 wolframalpha.com giving 31.01.2013 + 1 month as

PHP strtotime +1 month adding an extra month [duplicate]

旧时模样 提交于 2019-11-27 04:48:28
This question already has an answer here: PHP: Adding months to a date, while not exceeding the last day of the month 4 answers I have a simple variable that adds one month to today: $endOfCycle = date("Y-m", strtotime("+1 month")); Today is January 2013, so I would expect to get back 2013-02 but I'm getting 2013-03 instead. I can't figure out why it's jumping to March. It's jumping to March because today is 29th Jan, and adding a month gives 29th Feb, which doesn't exist, so it's moving to the next valid date. This will happen on the 31st of a lot of months as well, but is obviously more

How to get closest date compared to an array of dates in PHP

元气小坏坏 提交于 2019-11-27 04:25:16
This post almost answered this question for me, but I have a specific need and didn't find what I sought there. This lies right outside my experience; couldn't quite wrap my head around it, so all I really need is a point in the right direction. Let's say I have an array as follows: array(5) { [0]=> "2013-02-18 05:14:54" [1]=> "2013-02-12 01:44:03" [2]=> "2013-02-05 16:25:07" [3]=> "2013-01-29 02:00:15" [4]=> "2013-01-27 18:33:45" } I would like to have a way to provide a date ("2013-02-04 14:11:16", for instance), and have a function determine the closest match to this in the array (which

PHP, Get tomorrows date from date

拜拜、爱过 提交于 2019-11-27 03:54:52
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? 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 5.4+: echo (new DateTime('2013-01-22'))->add(new DateInterval("P1D")) ->format('Y-m-d H:i:s'); $tomorrow =

PHP strtotime returns a 1970 date when date column is null

青春壹個敷衍的年華 提交于 2019-11-27 03:18:46
问题 I want to display $row->depositdate in dd-mm-yyyy format. If the date column in database is null the date displayed is : 01-01-1970 echo "<td align=center>".date('d-m-Y', strtotime($row->depositdate))."</td>"; If the date is null in database it should display nothing , otherwise the date in dd-mm-yyyy format should be displayed. Thanks in advance Sandeep 回答1: NULL is interpreted as 0 by strtotime, since it want to be passed an integer timestamp. A timestamp of 0 means 1-1-1970. So you'll have

php check for a valid date, weird date conversions

时光毁灭记忆、已成空白 提交于 2019-11-27 03:17:06
问题 Is there a way to check to see if a date/time is valid you would think these would be easy to check: $date = '0000-00-00'; $time = '00:00:00'; $dateTime = $date . ' ' . $time; if(strtotime($dateTime)) { // why is this valid? } what really gets me is this: echo date('Y-m-d', strtotime($date)); results in: "1999-11-30", huh? i went from 0000-00-00 to 1999-11-30 ??? I know i could do comparison to see if the date is either of those values is equal to the date i have but it isn't a very robust

adding 30 minutes to datetime php/mysql

拟墨画扇 提交于 2019-11-27 00:16:45
问题 I have a datetime field (endTime) in mysql. I use gmdate() to populate this endTime field. The value stored is something like 2009-09-17 04:10:48. I want to add 30 minutes to this endtime and compare it with current time. ie) the user is allowed to do a certain task only 30 minutes within his endTime. After 30 minutes of his endTime, i should not allow him to do a task. how can i do that in php? i'm using gmdate to make sure there are no zone differences. Thanks in advance 回答1: If you are

Check if variable is a valid date with PHP

六月ゝ 毕业季﹏ 提交于 2019-11-26 22:55:45
问题 I am working on a script that will import some data from a CSV file. As I am doing this I want to be able to check a variable to see if it is a valid date string. I have seen several ways to check if a sting is a date, but most of them require you to now the format. I will not know the format that the date will in. right now I am using strtotime(), but this fails to easily $field ="May"; if(strtotime($field)){ echo "This is a date"; } In this case, "May" was the persons first name, and not a