Jump to content

Recommended Posts

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.

Link to comment
https://forums.phpfreaks.com/topic/177494-help-with-some-dates/
Share on other sites

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;
}
}

Link to comment
https://forums.phpfreaks.com/topic/177494-help-with-some-dates/#findComment-935850
Share on other sites

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

Link to comment
https://forums.phpfreaks.com/topic/177494-help-with-some-dates/#findComment-935856
Share on other sites

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];

Link to comment
https://forums.phpfreaks.com/topic/177494-help-with-some-dates/#findComment-935857
Share on other sites

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");

 

 

Link to comment
https://forums.phpfreaks.com/topic/177494-help-with-some-dates/#findComment-935860
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.