Get week number and year from a Unix timestamp in php [closed] - php

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 10 years ago.
I am trying to create a weekly calendar that allows the user to advance or go back a week. So far I have this...
<?
if(isset($_POST['add_week'])){
$last_week_ts = strtotime($_POST['last_week']);
$display_week_ts = $last_week_ts + (3600 * 24 * 7);
} else if (isset($_POST['back_week'])) {
$last_week_ts = strtotime($_POST['last_week']);
$display_week_ts = $last_week_ts - (3600 * 24 * 7);
} else {
$display_week_ts = floor(time() / (3600 * 24)) * 3600 * 24;
}
$week_start = date('d-m-Y', $display_week_ts);
$week_number = date("W", strtotime( $display_week_ts));
$year = date("Y", strtotime( $display_week_ts));
echo $week_start.' '.$week_number.' '.$year;
?>
<table name="week">
<tr>
<?
for($day=1; $day<=7; $day++)
{
echo '<td>';
echo date('d-m-Y', strtotime($year."W".$week_number.$day))." | \n";
echo '</td>';
}
?>
</tr>
<tr>
<form name="move_weeks" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<input type="hidden" name="last_week" value="<? echo $week_start; ?>" />
<td colspan="7"><input type="submit" name="back_week" value="back_week" /><input type="submit" name="add_week" value="add_week" />
</td>
</form>
</tr>
</table>
The the back and forward buttons work just fine and the $week_start variable which represents the first date of the week advances and goes back as it should but regardless of the date shown the $week_number and $year show as 01 and 1970 or 36 and 1600.
I know it must be something to do with the way I have tried to extract them from $display_week_ts but I don't know what

The following looks out of place:
$week_start = date('d-m-Y', $display_week_ts);
$week_number = date("W", strtotime( $display_week_ts));
$year = date("Y", strtotime( $display_week_ts));
See how you're using $display_week_ts in the first statement, but for the other (and similar) statements, you wrap that timestamp inside a call tostrtotime() which returns false.
It's best to just drop the strtotime() and use the variable as is:
$week_number = date("W", $display_week_ts);
$year = date("Y", $display_week_ts);

Ok, fixed it, I needed to get the week number and year from $week_start instead

Related

Calculating difference between days * price per day = value PHP [closed]

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 9 years ago.
I'm currently working on a little form that allows people to book a stay at a hostel. Basicly, I get them to pick two dates. (Day of arrival & day of departure) Then I want to calculate the difference between those dates in days, and finally I want to multiply the number of days with the price per day.
I can't seem to figure out how to calculate the days * price per day and pass that string on to the "amount value" in my form.
This is what I got so far:
<input type="text" id="datepicker" value="arrival (day-month-year)" />
<input type="text" id="datepicker2" value="departure (day-month-year)" />
<form action="checkout.php" method="post">
<input name="amount" type="hidden" value="$dateDiff * price per day = value">
<input type="submit" value="Check out" id="checkout">
</form>
And my PHP
<?php
$date1 = ("#datepicker");
$date2 = ("#datepicker2");
$dateDiff = $date1 - $date2;
$fullDays = floor($dateDiff/(60*60*24));
echo "Difference is $fullDays days";
?>
Edit: After the first two comments, let me explain that I'm fairly new to PHP. Thanks.
You have to put the input fields in your form and give it a name likes this:
<form action="checkout.php" method="post">
<input type="text" id="datepicker" name="datepicker" value="arrival (day-month-year)" />
<input type="text" id="datepicker2" name="datepicker2" value="departure (day-month-year)" />
<input name="amount" type="hidden" value="$dateDiff * price per day = value">
<input type="submit" value="Check out" id="checkout">
</form>
Now in your PHP file:
<?php
if(isset($_POST["datepicker"], $_POST["datepicker2"])){
$date1 = new DateTime($_POST["datepicker"]);
$date2 = new DateTime($_POST["datepicker2"]);
$diff = $date1->diff($date2);
$amount_per_day = 10; // for example 10 dollars
$total = floor($diff["days"]) * $amount_per_day; // total
}
?>
The datetime object offers a diff function:
http://php.net/manual/en/datetime.diff.php
$datetime1 = new DateTime('2009-10-11');
$datetime2 = new DateTime('2009-10-13');
$interval = $datetime1->diff($datetime2);
print_r($interval);
DateInterval Object
(
[y] => 0
[m] => 0
[d] => 2
[h] => 0
[i] => 0
[s] => 0
[invert] => 0
[days] => 2
)
So you could calculate your price with
$price = $pricePerDay * $interval->days;
Try this:
function dateDiff($start, $end)
{
$start_ts = strtotime($start);
$end_ts = strtotime($end);
$diff = $end_ts - $start_ts;
return round($diff / 86400);
}

Get Date after adding month and leave above days [closed]

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 9 years ago.
$start_date = "2012-01-05";
$end_date = "2012-02-10";
How to get month and after getting month date should like in above example
$end_date should "2012-02-05"; leave above 5 days. don't count a above day.
How can i do this in php?
I wan't to like
$month = 1;
$end_date = "2012-02-05";
Second Example
$start_date = "2012-06-19";
$end_date = "2012-09-25";
then should
$month = 3;
$end_date = "2012-09-19";
Leave days between "2012-09-19" to "2012-09-25".
Thanks
$start = '2012-01-23';
$months = 12;
$date=new DateTime($start);
$date->modify('+'.$months.' month');
echo $date->format('Y-m-d');
You can use strtotime() to just add a number of months to your original date...
$start = '2012-01-23';
$months = 3;
$timeExpr = sprintf('+%d months', $months);
$end = date($start, strtotime($timeExpr));
echo $end; //will output '2012-04-23'

How can I compute the next Xth day (of the month)? [closed]

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 10 years ago.
I want to compute the next Xth day in the calendar.
For example today is 2012-10-08. if X=25 i want it to return 2012-10-25 but if X=06 i want 2012-11-06. If the month doesn't have the required X days, it must return the last day of the month (if i'm looking for the 30th of february for example, it must return 28 or 29 if leap year)
It seems simple enough but I get caught by all the special cases (last month of the year, 28-31 day months, etc).
You can use strtotime() and t:
$x = 5; // given day
if(date('t') < $x){ // check if last day of the month is lower then given day
$x = date('t'); // if yes, modify $x to last day of the month
}
$month = date('m'); // current month
if(date('d') >= $x){ // if $x day is now or has passed
$month = $month+1; // increase month by 1
}
$year = date('Y'); // current year
if($month > 12){ // if $month is greater than 12 as a result from previous if
$year = date('Y')+1; // increase year
$month = 1; // set month to January
}
if(date('t', strtotime($year.'-'.$month.'-01')) < $x){ // check if last day of the new month is lower then given day
$x = date('t', strtotime($year.'-'.$month.'-01')); // if yes, modify $x to last day of the new month
}
$date = date('d F Y', strtotime($year.'-'.$month.'-'.$x));
// 05 November 2012
HERE is a nice tutorial.

How to display a formatted weekdays from a given Week number [closed]

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 10 years ago.
I'm getting the weekdays from a given week number and year using the php below:
$week_number = 42;
$year = 2014;
for($day = 1; $day<=7; $day++)
{
echo date('m/d/Y',strtotime($year."W". $week_number.$day));
}
The Output look likes this:
10/13/2014
10/14/2014
10/15/2014
10/16/2014
10/17/2014
10/18/2014
10/19/2014
How can I make it look just like this:
Oct 13 - Oct 19.
Thank you.
<?php
$week_number = 42;
$year = 2014;
echo date('M d',strtotime($year."W".$week_number . 1)) . " - " . date('M d',strtotime($year."W".$week_number . 7)).".";
?>
$week_number = 42;
$year = 2014;
$week_number = ($week_number < 10) ? '0'.$week_number : $week_number;
echo date('M d',strtotime($year.'W'.$week_number.'1')).' - '.date('M d',strtotime($year.'W'.$week_number.'7')).'.';
// remember that $week_number must be prefixed with 0 if week number is lower than 10
you can use strftime
here is the link http://php.net/manual/en/function.strftime.php
Then use
date("F j, D");for your date function

PHP calendar first week of the year displays wrong year [duplicate]

This question already has an answer here:
Closed 10 years ago.
Possible Duplicate:
Php.Advance weekly calendar one week
I have written a script that displays a calendar week by week that the user can chose to go back or forward a week at a time. Everything works great except the first week of every year still displays the wrong year and the 31st December shows as 02/01. It seems that only week 1 is affected, the days are correct again in week and onwards
<?
if(isset($_POST['add_week'])){
$last_week_ts = strtotime($_POST['last_week']);
$display_week_ts = $last_week_ts + (3600 * 24 * 7);
} else if (isset($_POST['back_week'])) {
$last_week_ts = strtotime($_POST['last_week']);
$display_week_ts = $last_week_ts - (3600 * 24 * 7);
} else {
$display_week_ts = floor(time() / (3600 * 24)) * 3600 * 24;
}
$week_start = date('d-m-Y', $display_week_ts);
$week_number = date("W", $display_week_ts);
$year = date("Y", $display_week_ts);
echo $week_start.' '.$week_number.' '.$year;
?>
<table name="week">
<tr>
<?
for($day=1; $day<=7; $day++)
{
echo '<td>';
echo date('d-m-Y', strtotime($year."W".$week_number.$day))." | \n";
echo '</td>';
}
?>
</tr>
<tr>
<form name="move_weeks" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<input type="hidden" name="last_week" value="<? echo $week_start; ?>" />
<td colspan="7"><input type="submit" name="back_week" value="back_week" /><input type="submit" name="add_week" value="add_week" />
</td>
</form>
</tr>
</table>
Any help will be appreciated
As suggested in the answer to the original question, you need to not use the week number and year number stuff.
If at the end of the day, you want to be able to list seven days for the week, just do not use this section of code at all:
$week_number = date("W", $display_week_ts);
$year = date("Y", $display_week_ts);
echo $week_start.' '.$week_number.' '.$year;
And replace the loop that echos out with the loop suggested in the original question.
This has to do with the format you are providing date(). Specifically W.
From the PHP Docs:
ISO-8601 week number of year, weeks starting on Monday
It is coincidence this is appearing because December 31, 2012 is a Monday.
You should rewrite your code to use other ways to calculate dates. I'd recommend against trusting form data.

Categories