Does anyone know of a php class or library that helps deal with dates (meaning weekdays)? For example, I could use it to say "give me the timestamp for next Tuesday at 9:00" or "is this timestamp between next Tuesday and next Thursday".
I just have a part of my application that I need to make sure I'm not scheduling things at certain times.
There is the built-in DateTime class, but you don't even need it for this:
//this gives results in the default timezone
//timestamp for next Tuesday at 9:00
strtotime("next tuesday 09:00");
//is this timestamp between next Tuesday and next Thursday
$ts >= strtotime("next tuesday") && $ts < strtotime("next friday")
Can you use strtotime()?
http://php.net/strtotime
If you must use a class Zend_Date is quite powerful. It comes with the Zend framework but is pretty standalone as far as I know. No obligation to implement MVC and all the other Zend stuff.
Some of the comments in the PHP manual explain how to do this with date.
Related
I need to know the date of next Friday. Known only today's date.
PHP / Framework Laravel 5.1
How do I know the date of the next Friday?
Thank you!
This is nice and easy using strtotime
strtotime('next friday');
See strtotime()
I have a very simple question I would like to pose, but the answer is eluding me. I have a date (such as "first sunday in April") and I am trying to find the day before this, using the php strtotime function. It occurred to me that this would be harder using any other bespoke method as I'd have to take into account the number of days in a month in case (for instance), the first sunday in April was the 1st. Ive tried the obvious solutions like "first sunday in April - 1 day" and "1 day before first Sunday in April".
I'm hoping it can be done in one step rather than find the output of strtotime and then perform arithmetic on its results.
I'm sure it must be simple. Many thanks!
Try the different relative date/time formats at:
http://php.net/manual/en/datetime.formats.relative.php
EDIT: Two minuses in under a minute. I thought this was a tiny but interesting query.
ORIGINAL QUERY: I am generating a five week calender into which I will pour info (the day and date and other stuff).
I want the top left cell to be the "current" Sunday.
For example if today is Weds 12th then I need to find Sun 9th as the start for the run. Then I just do a $var = strtotime("+1 day", $var) for the next 34 slots.
My problem is doing this neatly if today is Sunday.
At present I have:
date_default_timezone_set("Pacific/Honolulu");
$day_now = time();
$current_day= date ("D", $day_now);
if ($current_day == "Sun")
{$day_now = strtotime("+1 day", $day_now);}
$day_now = strtotime("last sunday");
//do stuff/
I just wondered if there was a more "tidy" way of doing this.
I tried "this sunday, last sunday, sunday this week" but could find nothing that would pick today as the Sunday and ALSO work for the rest of the week.
Just curious if anyone has found a form of words that works for this with strtotime.
OK strtotime has NO one phrase solution to the current query ('this sunday" for the whole week). (I would call it a bug!)
The linked discussions
Strange behaviour of strtotime() when using relative dates ('this week')
and
Computing relative dates in php using strtotime()
offer solutions.
I like my current solution - while not elegant it is very readable and I know that if I come back to it in a couple of years I will understand what is going on.
Hopefully "this sunday" will be fixed someday instrtotime.
Thanks for the input.
My query is for example this:
$query = "... WHERE timestamp > :thisMonday AND timestamp <= :lastMonday";
$query_params[":thisMonday"] = date('Y-m-d', strtotime('monday this week'));
$query_params[":lastMonday"] = date('Y-m-d', strtotime('monday last week'));
With this query I get the result from last week (starting from last week monday to last week sunday).
I've found a MySQL solution for this too but either they don't work properly (for example last monday is literally the last monday not the last week monday) or they are pretty complex for a simple task like this.
I've found this:
https://online.promoters.ro/web-design-development/mysql-curdate-and-now-vs-php-date-heavy-server-load.html
Which says to always use PHP.
And this: Faster to use MySQL's CURDATE() or PHP's date()?
Which says it depends. But I am not sure what the best way for my task is.
Just to give you an answer, even if it may be wrong, this is just my opinion.
The first link you provided: https://online.promoters.ro/web-design-development/mysql-curdate-and-now-vs-php-date-heavy-server-load.html; I thought was completely biased and provided no proof and really said nothing to me except it caused a server problem; but I don't think he took into consideration on other data at play while he was testing, or if it could have been a different problem. There is also no date of when the article was written, so we can only guess what MySQL server version he was using.
My opinion is to keep the date format in the original language. So from Php -> MySQL -> Php. I think that might actually be the best and safest way. Another opinion is that it is easier to manipulate the time variable.
I implemented a countdown in one of our projects. As this was my first countdown I just realized that if I tell PHP:
$timeleft = strtotime('2013-10-31') - strtotime('now');
$daysleft = floor( $timeleft/(24*60*60) );
I get the time left to 2013-10-31 00:00 instead of the full day.
Why is it this way? I expected 2013-10-31 23:59:59 or 2013-11-01 00:00
As an example from daily life: I have a rental contract for a car until 2013-10-31, or any contract, then I know that the rental period always includes the final day completely.
This might seem like a beginner's question but I would like to know the reason for this decision. Thank you for your ideas and experience.
PS: Is it the same in all programming languages?
Then give a time to strtotime as well.
$timeleft = strtotime('2013-10-31 23:59:59') - strtotime('now');
Why?
Because when you say:
strtotime('2013-10-31');
That means the exact timestamp when that date started, which is equivalent to saying:
strtotime('2013-10-31 00:00:00'); // hence the missing 24 hours
Edit
In fact, saying strtotime('now') is just like saying time(), so you can remove that as well. And have it like:
$timeleft = strtotime('2013-10-31 23:59:59') - time();
PS: Is it the same in all programming languages?
Its not a language limitation by any means, in any language. Its how you interpreted the function which generates time for you. After interpreting it correctly, send it the right values and it will work as you expect.
The reason for this will be the same as it is for money:
1 EURO = 1.00 EURO and not 1.50 EURO.
If the time of day matters, you should define it in both objects.
As dates can be ambiguous, varying on time zones, daylight savings - even the architecture of the system that its running on, it is recommended to use the built in DateTime object.
There are numerous methods that can be used to manipulate the date, such as DateTime::diff() without all the manual calculations (ie. floor( $timeleft/(24*60*60) );).
So they might offer you better flexibility and accuracy when comparing date intervals and/or adding them together.
$date = new DateTime('2013-10-31');
$now = new DateTime('today');
$diff = $date->diff($now);
$new = $date->add($diff);
echo $new->format('Y-m-d H:i'); // 2013-11-01 00:00