How do I get the next day of the week? - php

Like if today is Thursday, then I want to display Friday using php date() function.
For example, the below code will show the current month but the next day of the month. It worked. <?php echo date("M "), date("d")+1 ?>
So I was trying through this one <?php echo date("l")+1, date("d")+1 ?> and it fooled me :(.

Have a look at strtotime. Also, an l will produce a full textual representation of the day of the week (e.g. Friday) when using date.
Something like the following should do what you want:
date('l', strtotime('+1 day'))

The following expression gives you the next day:
date('l', time()+3600*24)

Related

Get day of the next week by monday with php

i want to get the day of the next week with php..i need something like this
If today is monday, i want to display the days (es 18-19-20) of the next week, not of this week..the week start on monday...but it will work also if today is sunday..always the days of the next week..i can't found anything because i tried to use jddayofweek but i don't understand it maybe..i haven't found an italian guide..
I have a table which display the days in stringn form..so sunday monday etc, fir my problem i tried date() function but if i use +1 on the day i will have problem with february for example
Not sure exactly what you're doing, but maybe something like:
echo date("d", strtotime("monday next week"))
This will give you next Monday:
$date = date_create()->modify('monday next week');
echo date_format($date, "d");

How can I get name of month after the current month?

The following gives me October
echo date('F');
I'm using the following code to give me the name of the coming month (the month after this one), so I'm hoping to see November.
$nextmonth = date("F",strtotime("+1 months"));
Today is 31st Oct, but the above gives 'December' What am I doing wrong? How can I get the month after the current month?
Use relative formats to get the first day of the next month:
echo (new DateTime('first day of next month'))->format('F');
The reason why you see this problem is when adding time to a date at the end of the month you run into issues with months having fewer than 31 days. This can cause you to skip a month. The best bet is to always start your date math at the beginning of the month before adding time to it or just relative formats as demonstrated above.
PHP 5.3:
$nextMonth = new DateTime('first day of next month');
echo $nextMonth->format('F');

Getting day of week from timestamp PHP

i am trying to get the day of week from a timestamp:
an example of the timestamp could be:
2014-09-14 18:28:11
I have tried with the following code:
$date = date("D", strtotime($activity[$i]['timestamp']));
However the result i get here is:
Thu
which should have been sunday?
Also is it possible to get it as a full discription instead of a short version of the day name?
Answer to part two of the question is that you can just use l (lowercase 'L') and it'll output Sunday instead of Sun.
$date = date("l", strtotime($activity[$i]['timestamp']));
As for the first part, it probably output Thursday, 1 January 1970 because it received an error instead of an actual date as argument to strtotime.

Select between the 8th of this month and the 8th of last month, for every month

How would you select all records between the 8th of this month and the 8th of last month without passing in the current month? My date column is date type.
SELECT *
FROM table
WHERE date BETWEEN '8th of last month' AND
'8th of this month'
I've tried working out the date calculations in PHP then sending them to MySQL. But it gets a bit complicated when factoring in years. I'm wondering if this could be accomplished with SQL instead?
Using PHP might be more straightforward from a readability standpoint using relative time with strtotime().
The following should get you started:
echo date('Y-m-d', strtotime('eighth of last month'));
UPDATE
Ordinals don't work as I thought above. However, assuming you use the same day of the month (i.e 8th). The following is fine:
echo date('Y-m-08', strtotime('last month'));
Here is a solution
echo date('Y-m-d', mktime(0, 0, 0, date('n') - 1, 8));
If you want to use strtotime
echo date('Y-m-d', strtotime('+7 days', strtotime('first day of last month')));

PHP strtotime() is not returning the correct month

Being that the current month/year is January 2012, why does the following code return December 2011 and not November 2011?
echo date("F Y", strtotime("-2 months"));
This is on PHP 5.3.0 if it makes a difference.
To get what you are looking for you can use this rather verbose version instead:
echo date("F Y", strtotime("first day of this month - 2 months"));
The problem with your original version is described in detail here: http://derickrethans.nl/obtaining-the-next-month-in-php.html. Quoted below:
Over and over again PHP users complain that next month in PHP's
date-string parser doesn't go to the next month, but instead skips to
the one after next month; like in the following example:
<?php
$d = new DateTime( '2010-01-31' );
$d->modify( 'next month' );
echo $d->format( 'F' ), "\n";
?>
The output of the little script will be March. March obviously doesn't
follow January as February is in between. However, the current
behavior is correct. The following happens internally:
next month increases the month number (originally 1) by one. This
makes the date 2010-02-31. The second month (February) only has 28
days in 2010, so PHP auto-corrects this by just continuing to count
days from February 1st. You then end up at March 3rd. The formatting
strips off the year and day, resulting in the output March. This can
easily be seen when echoing the date with a full date format, which
will output March 3rd, 2010:
This is for adding months, but the same applies in reverse when subtracting months; there was no November 31st, so the strtotime method "corrects" it into December 1st.

Categories