calculating calendar week php [duplicate] - php

This question already has an answer here:
Week number and Week day
(1 answer)
Closed 9 years ago.
If anyone has an idea how can I calculate a calendar week depending on given date in php, for example 06.06.2013 (format like this 2013-06-06). is calendar week 23. I was searching for the solution all morning and found nothing useful. Any help or link, anything would be appreciated. Thank you

Use date("W")
echo date("W", strtotime('2013-06-06'));
See it in action

date('W') should give you the week of the year. RTM
If you don't have your time as a unix timestamp you can use strtotime() first and pass it as the second parameter
ex. date('W',strtotime($my_time_string))
You can see the PHP manual here

Like this:
$time = '2013-06-06';
$calendar_week = date('W', strtotime($time));

Related

Get next 2 days not counting current day in PHP [duplicate]

This question already has answers here:
PHP Strtotime without current time?
(2 answers)
Closed 4 months ago.
I have an Unix timestamp like this 1660293621 (2022-08-12 8:40). I want to get next 2 days not counting current date. I expect the result to be 2022-08-15 00:00.
I tried
strtotime("+3 Days", $current_date)
but it returns 2022-08-15 8:40, not 00:00
How can I get that in PHP? Thank you~
$Today=date('y:m:d');
// add 3 days to date
$NewDate=Date('y:m:d', strtotime('+3 days'));
Reference:
Increase days to php current Date()
I figured it out, just add 0:00 will help
$next2days = strtotime("+3 days 0:00", $current_date);

Subtract seconds from a specific given time PHP [duplicate]

This question already has answers here:
Subtract one second from a given time
(2 answers)
Closed 8 years ago.
I writing a code for subtract seconds from a time using php. i have date which assigned to variable , i need to subtract seconds from that date.
$date="2014-03-16 17:40:27";
echo date("Y-m-d H:i:s", strtotime($date) - strtotime("-600 seconds"));
but this gives me dates on 1970S, i search everhere and didn't found a answer which matched for my question. can anyone help me to fix this little code
strtotime() gives you a timestamp in seconds. Don't make another timestamp to subtract from it, just take 600 from it:
echo date("Y-m-d H:i:s", strtotime($date) - 600);
//2014-03-16 17:30:27

Get last date PHP [duplicate]

This question already has answers here:
How can I find the first and last date in a month using PHP?
(12 answers)
Closed 9 years ago.
I want to know how to get LAST DAY when I just know month and year. The month is integer and year is integer too. This is the codes :
<?php
$month = 2;
$year = 15; //I don't know why after "date('y',strtotime($tanggal_awal))" I get 15 not 2015
//how to know last date ????
?>
The real source is not like this. It's very long. I want to know PROPER MANNER TO GET last day WHERE MONTH IS FEBRUARY AND YEAR IS 2015.
You can use the cal_days_in_month() function, the total number of days is the same as what the last day will be.
You may need to install the calendar functions in PHP, follow instructions here: http://www.php.net/manual/en/calendar.installation.php
As suggested in the comments you can also use date('t'), which doesn't require the above extension to be installed.
use this to get the number of days of the desired month...
int cal_days_in_month ( int $calendar , int $month , int $year )
Which gives the last date of the month.
cal_days_in_month

PHP get the date of this weeks monday [duplicate]

This question already has answers here:
Get first day of week in PHP?
(39 answers)
Closed 9 years ago.
Any way to take a date like this 2013-05-29 in PHP and get the date (in the same format) of that weeks monday? So the output would be like this: 2013-05-27
date('Y-m-d', strtotime('last sunday +1 day', strtotime('2013-05-29')));
last sunday +1 day because last monday would return the Monday of the previous week if $timestamp actually was a Monday already.
And parsing of the original date value 2013-05-29 in a second step because all together as one argument does not work well (mixing of absolute and relative date values is something strtotime does not like very much).

custom datetime, add 30 days to current date [duplicate]

This question already has answers here:
Add number of days to a date
(20 answers)
Closed 9 years ago.
I want to add 30 days to current date time.
for example :
$today = date("Y-m-d H:i:s");
I want to generate new date time , I want to add 30 days to $today date time.
How I do that?
UPDATE:
special for those 5 supernova smart guys: John Conde, Woot4Moo, cryptic ツ, moonwave99, Marc B ; yes it is duplicate but people formulate their question differently and this post may help other people to find faster than other post which was formulated differently but have same main idea. I would not mark this as trash, people do think different and use different key words during searching, and somebody may find this post quicker than the other duplicate post... have fun!
$thirty_days_ahead = date('Y-m-d H:i:s', strtotime("+30 days") )
try this
$today = date('Y-m-d H:i:s',strtotime("+30 days"));
strtotime("+30 days")
should be the most simple way to do it.

Categories