How to get starting date from week number - php

Getting week number this format :
$weeknumber = date('W', strtotime('10-02-2015'));
How to get starting date from this weeknumber.

you can use this
echo $weeknumber = date('W', strtotime('18-10-2012'));
$gendate = new DateTime();
$gendate->setISODate(2015,$weeknumber,4); //year , week num , day
echo $gendate->format('d-m-Y'); //"prints"

Use this
you need to change like this strtotime('10-02-2015'))
$date_string = "2012-10-18";
$weeknumber = date("W", strtotime($date_string));
//get the date from week number
isodate
$gendate = new DateTime();
$gendate->setISODate(2013,$weeknumber ,4); //year , week num , day
echo $gendate->format('d-m-Y'); //"prints" 26-12-2013

Don't mix strtotime() and date() functions with DateTime class. It can be simply done without them, and with only DateTime, like:
$date = new DateTime('10-02-2015');
$date->setISODate($date->format('o'), $date->format('W'));
echo $date->format('d-m-Y');
demo

Related

get next immediate same date using php

i need to get immediate date like if i select date is 05 then output will be 2017-07-05 cause select date already passed
if i select 12 then output will be 2017-06-12 cause this date future date
final if i select previous date of current month then output will be next month same date and if i select future date of current month then output will be same month
i have tired but not working this
$today = date("Y-m-d");
$next_payment_date = date('Y-m-d', strtotime('+1 month', $today));
or
$time = time();
date("Y-m-d", mktime(0,0,0,date("n", $time),date("j",$time)- 1 ,date("Y", $time)));
thanks in your advance
One more option:
https://3v4l.org/Me2Kh
$input = 12;
$day = date("d");
if ($input > $day){
$date = date("Y-m-"). str_pad($input,2,"0", STR_PAD_LEFT);
}else{
$date = date("Y-m-",strtotime("+1 month")). str_pad($input,2,"0", STR_PAD_LEFT);
}
echo $date;
I use str_pad to keep two digit day number.
Try this -
<?php
$day = '05';
$today = date('Y-m-d');
$supplied = date('Y-m-'.$day);
if($today>$supplied){
$final = date('Y-m-d', strtotime("+1 months", strtotime($supplied)));
}
else{
$final = $supplied;
}
echo $today;
echo '<br />';
echo $supplied;
echo '<br />';
echo $final;
What I'm doing here -
Comparing the current and supplied date
Based on comparison, if supplied date is smaller, I'm adding 1 month else dislpaying the supplied date.
use this,
$today = date('Y-m-d');
$nextDate = date('Y-m-d', strtotime('+1 month'));
or $nextDate = date('Y-m-d', strtotime('+1 month', strtotime($today));
I think this may help.
I would consider using DateTime and it's add method for a DateInterval.
$date = new \DateTime('now', new \DateTimeZone('America/New_York'));
$interval = new \DateInterval('P1M');
$date->add($interval);
Here are the supported DateTimeZone values. Make sure to set that to the applicable time zone.
Edit:
DateTime is mutable, so please keep that in mind.
Try this code :
<?php
$selected_date = '2017-06-05';
$current = date('Y-m-d');
//echo $current;
if($selected_date < $current)
{
$newDate = date('Y-m-d',strtotime($selected_date."+1 month"));
echo $newDate; // gives 2017-07-05
}else if($selected_date > $current)
{
$newDate = $current;
echo $newDate; // gives 2017-06-07
}
?>
From what you described in the points at beginning of the question, you could achieve it this way:
$selectedDate = new DateTime('2016-06-05 00:00:00');
$now = new DateTime('now');
$now->setTime(0, 0, 0);
if ($selectedDate < $now) { // Selected date is in past
// Set month and year to current
$selectedDate->setDate(
date('Y'),
date('m'),
$selectedDate->format('d')
);
// Add 1 month
$selectedDate->add(new DateInterval('P1M'));
}
// If selected date is current or in future we do nothing
echo $selectedDate->format('Y-m-d');
For input 2017-06-05 it will return 2017-07-05 as expected, and for current or future date will return the date that was selected. Works also for any past date like 2016-04-05

php: Calculate Date from Week of Year and Day of Week

I am currently struggling with the following: I need to get the Date like "Sun, 29.05.2016 18:00" but all i have is the number of the week from the beginning of the year, the number of the day of week and the hour of the day.
Week: 21
Day: 7
Hour: 18
Now my question is how do i get the actual date and time with php's date functions or own code?
You are looking for DateTime::setISODate
$week_no = 21;
$day =7;
$hour = 18;
$d= new DateTime();
$d->setISODate(date('Y'),$week_no, $day);
$d->setTime ($hour,0);
echo $d->format('D, d.m.Y H:i');
Pretty straightforward with DateTime objects:
$week = 21;
$day = 7;
$hour = 18;
$dateFormat = sprintf('%d-W%02d-%d', (new DateTime())->format('Y'), $week, $day);
$x = new DateTime($dateFormat);
$x->setTime($hour, 0);
echo $x->format('Y-m-d H:i:s');
(assuming the current year)
Try with setISODate and DateTime. Online Check
Setting the 2016 from your desire output and use the hour directly to the format.
$gendate = new DateTime();
$gendate->setISODate(2016, 21, 7); //year , week num , day
echo $gendate->format('D, d.m.Y 18:00'); //Sun, 29.05.2016 18:00

php get date from weeknumber and day number [duplicate]

I'm trying to get the date from the week number, day number and year.
For eg:
week number = 52
day number = 4 (of week 52)
year = 2013
In this case, the date should be 26-12-2013.
How can I do it using PHP? I've already tried with strtotime(), but I'm confused about the formats. Can someone help?
Make use of setISODate()
<?php
$gendate = new DateTime();
$gendate->setISODate(2013,52,4); //year , week num , day
echo $gendate->format('d-m-Y'); //"prints" 26-12-2013
Try this code.
<?php
function change_date($week_num, $day) {
$timestamp = strtotime(date('Y') . '-W' . $week_num . '-' . $day);
return $timestamp;
}
$timestamp = change_date(52, 4);
echo date('d-m-Y', $timestamp);
?>
You can also use the strtotime function. In your example, you can write:
date("Y-m-d", strtotime("Y2013W52-4")) // outputs: 2013-12-26
The strtotime will give you a timestamp that you can use in combination withe the date function.

Need to find future date using PHP

I need to find future date using php.But using php, I am unable to get correct date for February.I posted my code below.I need end date of February.Thanks in advance.
<?php
$date = '2011-12-31';
$dateOneMonthAdded = strtotime(date("Y-m-d", strtotime($date)) . " +2 month");
$end_date=date("Y-m-d",$dateOneMonthAdded);
echo date("Y-m-d",strtotime(date("Y-m-d", strtotime($end_date))));
?>
You can use the 1st of March and subtract 1 day:
$date = date_create('2012-03-01');
$date->modify("-1 day");
echo $date->format("Y-m-d");
Try this code.
<?php
$date = '2011-12-31';
$dateOneMonthAdded = strtotime(date("Y-m-t", strtotime($date)) . " +2 month");
$end_date=date("Y-m-t",$dateOneMonthAdded);
echo date("Y-m-d",strtotime(date("Y-m-d", strtotime($end_date))));
?>
If you absolutely need to use strtotime, you could try with this:
<?php
$date = '2011-12-31';
$dateOneMonthAdded = strtotime(date("Y-m-d", strtotime($date)) . ", next month, last day of next month");
$end_date=date("Y-m-d",$dateOneMonthAdded);
echo date("Y-m-d",strtotime(date("Y-m-d", strtotime($end_date))));
?>
Which will return what you expect: 2012-02-29.
You can take a look at relative formats for strtotime in the PHP Manual as well.
$dt = new DateTime();
$dt->setDate(2011, 12, 31);
$dt->modify('last day of +2 month');
//or
$dt->modify('+2 month -2 day');
//or
$dt->modify('next month last day of next month');
print_r ($dt);
use the below code to find the first/last day of a given month,
<?php
function findFirstAndLastDay($anyDate)
{
//$anyDate = '2009-08-25'; // date format should be yyyy-mm-dd
list($yr,$mn,$dt) = split('-',$anyDate); // separate year, month and date
$timeStamp = mktime(0,0,0,$mn,1,$yr); //Create time stamp of the first day from the give date.
$firstDay = date('D',$timeStamp); //get first day of the given month
list($y,$m,$t) = split('-',date('Y-m-t',$timeStamp)); //Find the last date of the month and separating it
$lastDayTimeStamp = mktime(0,0,0,$m,$t,$y);//create time stamp of the last date of the give month
$lastDay = date('D',$lastDayTimeStamp);// Find last day of the month
$arrDay = array("$firstDay","$lastDay"); // return the result in an array format.
return $arrDay;
}
//Usage
$dayArray=array();
$dayArray=findFirstAndLastDay('2009-02-25');
print $dayArray[0];
print $dayArray[1];
?>
<?php
$date = '2011-12-31';
$tmp_date = strtotime(date("Y-m-1", strtotime($date)) . " +2 month");
$end_date=date("Y-m-t",$tmp_date);
echo date("Y-m-d",strtotime(date("Y-m-d", strtotime($end_date))));
?>
If you are using PHP >= 5.2 I strongly suggest you use the new DateTime object. For example like below:
$date = '2011-12-31';
$end_date = new DateTime($date);
$end_date->modify('last day of +2 month');
echo $end_date->format('Y-m-d');
Live Demo
OR
Use strtotime()
$date = '2011-12-31';
$dateOneMonthAdded = strtotime(date("Y-m-d", strtotime($date)) . ", last day of +2 month");
echo $end_date = date("Y-m-d",$dateOneMonthAdded);

php - find date for same day of week for last year

So, in PHP i'm trying to return a date value for last year based on the same day of week.
EX: (Monday) 2011-12-19 inputted should return (Monday) 2010-12-20.
I was just doing it simply by -364 but then that was failing on leap years. I came across another function :
$newDate = $_POST['date'];
$newDate = strtotime($newDate);
$oldDate = strtotime('-1 year',$newDate);
$newDayOfWeek = date('w',$oldDate);
$oldDayOfWeek = date('w',$newDate);
$dayDiff = $oldDayOfWeek-$newDayOfWeek;
$oldDate = strtotime("$dayDiff days",$oldDate);
echo 'LAST YEAR DAY OF WEEK DATE = ' . date('Ymd', $oldDate);
however, that is failing when you try to input a Sunday date, as it does a 0 (sunday) minus 6 (saturday of last year date), and returns with a value T-6. IE inputting 2011-12-25 gets you 2010-12-19 instead of 2011-12-26.
I'm kind of stumped to find a good solution in php that will work for leap years and obviously all days of the week.
Any suggestions?
Thanks!
How about this, using PHP's DateTime functionality:
$date = new DateTime('2011-12-25'); // make a new DateTime instance with the starting date
$day = $date->format('l'); // get the name of the day we want
$date->sub(new DateInterval('P1Y')); // go back a year
$date->modify('next ' . $day); // from this point, go to the next $day
echo $date->format('Ymd'), "\n"; // ouput the date
$newDate = '2011-12-19';
date_default_timezone_set('UTC');
$newDate = strtotime($newDate);
$oldDate = strtotime('last year', $newDate);
$oldDate = strtotime(date('l', $newDate), $oldDate);
$dateFormat = 'Y-m-d l w W';
echo "This date: ", date($dateFormat, $newDate), "\n";
echo "Old date : ", date($dateFormat, $oldDate);
That gives:
This date: 2011-12-19 Monday 1 51
Old date : 2010-12-20 Monday 1 51
Use strtotime() to get a date, for the same week last year.
Use the format {$year}-W{$week}-{$weekday}, like this:
echo date("Y-m-d", strtotime("2010-W12-1"));
And you can do that for as long back you wan't:
<?php
for($i = 2011; $i > 2000; $i--)
echo date("Y-m-d", strtotime($i."-W12-1"));
?>
Make it easier :)
echo date('Y-m-d (l, W)').<br/>;
echo date('Y-m-d (l, W)', strtotime("-52 week"));
Edit: I forgot to write output: :)
2015-05-06 (Wednesday, 19)
2014-05-07 (Wednesday, 19)
<?php
$date = "2020-01-11";
$newdate = date("Y-m-d",strtotime ( '-1 year' , strtotime ( $date ) )) ;
echo $newdate;
?>
ref https://www.nicesnippets.com/blog/how-to-get-previous-year-from-date-in-php

Categories