rondog Posted October 13, 2009 Share Posted October 13, 2009 I have a list of dates in an array: $mondays = array( strtotime("October 12, 2009"), strtotime("October 19, 2009"), strtotime("October 26, 2009"), strtotime("November 2, 2009"), strtotime("November 9, 2009"), strtotime("November 16, 2009") ); It is an array of the next few mondays. I am wondering how I could figure out what the next monday in the array is depending on the current time, using time(); I cannot use strtotime("next monday") or strtotime("+1 monday") because the server I am working on is using a very old version of PHP (4.3.10-22) and it has a bug when I use strtotime("next monday");...I am also not able to upgrade the version of PHP. Quote Link to comment https://forums.phpfreaks.com/topic/177494-help-with-some-dates/ Share on other sites More sharing options...
rondog Posted October 13, 2009 Author Share Posted October 13, 2009 I found this to work, although I am sure their is a simpler way. $mondays = array( strtotime("October 12, 2009"), strtotime("October 19, 2009"), strtotime("October 26, 2009"), strtotime("November 2, 2009"), strtotime("November 9, 2009"), strtotime("November 16, 2009") ); for ($i = 0; $i < count($mondays); $i++) { if ($mondays[$i] > time()) { echo $mondays[$i] . " is greater than " . time() . "<br/>"; break; } } Quote Link to comment https://forums.phpfreaks.com/topic/177494-help-with-some-dates/#findComment-935850 Share on other sites More sharing options...
mikesta707 Posted October 13, 2009 Share Posted October 13, 2009 assuming your array will always be in order, this is probably the only way, since you can't use strtotime() in the way that would make your task a lot easier. However, if your array has even the slightest chance of having non- monday dates, you may run into problems. i think you need a new host Quote Link to comment https://forums.phpfreaks.com/topic/177494-help-with-some-dates/#findComment-935856 Share on other sites More sharing options...
.josh Posted October 13, 2009 Share Posted October 13, 2009 Alternate way to do it. Dunno which is better per se... $mondays = array( strtotime("October 12, 2009"), strtotime("October 19, 2009"), strtotime("October 26, 2009"), strtotime("November 2, 2009"), strtotime("November 9, 2009"), strtotime("November 16, 2009") ); $t = time(); $mondays[] = $t; sort($mondays); $k = array_search($t); echo $mondays[$k+1]; Quote Link to comment https://forums.phpfreaks.com/topic/177494-help-with-some-dates/#findComment-935857 Share on other sites More sharing options...
.josh Posted October 13, 2009 Share Posted October 13, 2009 anyways, where is this array of dates coming from? From a db? How about doing the math in the query instead? Quote Link to comment https://forums.phpfreaks.com/topic/177494-help-with-some-dates/#findComment-935859 Share on other sites More sharing options...
rondog Posted October 13, 2009 Author Share Posted October 13, 2009 assuming your array will always be in order, this is probably the only way, since you can't use strtotime() in the way that would make your task a lot easier. However, if your array has even the slightest chance of having non- monday dates, you may run into problems. i think you need a new host Trust me!! I know, its not my host..its my client's client's client's host or something like that so I have no control over it. I told my client to tell hers to upgrade but apparently the host deleted their web site when they tried to add a database so they dont want to do that anymore. It worked fine on my php5 server..this one has been hell though. My array will always be in order..its a contest that is running for 6 weeks so after that I wont need to worry about these dates. @crayon..I hardcoded the dates in. they are static. I would not normally do this, but this was supposed to launch last night and its doing fine for the time being, but when the next monday rolls around it would break because of strtotime("next monday"); Quote Link to comment https://forums.phpfreaks.com/topic/177494-help-with-some-dates/#findComment-935860 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.