bmwlaval
asked on
Subtracting time using JS function and PHP date functions
I'm trying to create a calendar style program which is Monday to Friday, where you can browse between weeks not including saturday and sunday. I haven't even gotten to the actual calendar part yet, I am trying to get the function for counting backwards and forwards with my dates to work.
So far I am using the strtotime date function in PHP, but right now I can only count forward and it only works for one cycle forward(7 days), then it won't go forward anymore.
I've attached my code below of the javascript function that I have created. I'm calling using an onclick event in html. And I am passing the current day and month to it.
I also want to create a goNextWeek(day, month) function at some point.
Can someone tell me what I am doing wrong?
Thanks for the help in advance!
So far I am using the strtotime date function in PHP, but right now I can only count forward and it only works for one cycle forward(7 days), then it won't go forward anymore.
I've attached my code below of the javascript function that I have created. I'm calling using an onclick event in html. And I am passing the current day and month to it.
I also want to create a goNextWeek(day, month) function at some point.
Can someone tell me what I am doing wrong?
Thanks for the help in advance!
<script>
function goLastWeek(day, month){
}
if(day <= 7){
month = month - 1;
}
var newday = 0;
newday = <?php echo strtotime("-7 day",strtotime(day));?> ;
document.location.href = 'cal.php?month='+month+'&day='+newday;
}
</script>
ASKER
So even though the js code is being called everytime I click the button, the php code will only be parsed once? Cause it should parse it when I click the button, and everytime I click the button, shouldn't it?
If you view the source code (browsersource) you will see what is going on.
The PHP code is only being parsed once (when opening the page) and not everytime you click the button.
The PHP code is only being parsed once (when opening the page) and not everytime you click the button.
ASKER
I don't see how that strtotime js function is going to help me, it is a function that takes a time stamp and returns a time stamp.
It doesnt parse just the number of days?
I tried integrating it into my code, but realised what was going to be the outcome. Is there any other way?
It doesnt parse just the number of days?
I tried integrating it into my code, but realised what was going to be the outcome. Is there any other way?
ASKER
So I guess I'm not understanding what you are suggesting I do. I'm attaching the entire code. It is not long, besides the java that I have already given you, my html functionality is just to test this java function.
If you could tell me what I am not understanding or doing wrong in my code, I would greatly appreciate it! Thank you!
You can ignore the commented lines.
If you could tell me what I am not understanding or doing wrong in my code, I would greatly appreciate it! Thank you!
You can ignore the commented lines.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<script src="php.js" type="text/javascript">
<script>
function goLastWeek(day, month){
// If the month is January, decrement the year
if(month == 1){
//--year;
month = 13;
}
if(day <= 7){
month = month - 1;
}
var newday = 0;
newday = strtotime('-7 day',day);
//newday = <?php echo date("j",day); ?>;
document.location.href = 'cal.php?month='+month+'&day='+newday;
}
</script>
</head>
<body>
<?php
$day = (isset($_GET["day"])) ? $_GET['day'] : "";
$month = (isset($_GET["month"])) ? $_GET['month'] : "";
$year = (isset($_GET["year"])) ? $_GET['year'] : "";
if(empty($day)){ $day = date("j"); }
if(empty($month)){ $month = date("n"); }
if(empty($year)){ $year = date("Y"); }
if(empty($letterday)){ $letterday = date("D"); }
echo "Letter day: $letterday";
echo "<br>";
echo "Day: $day";
echo "<br>";
echo "Month: $month";
echo "<br>";
echo "Year: $year";
//echo "<br><br>";
//echo date('Y-m-d', strtotime('-24 days'));
?>
<br />
<input type="button" value=" < " onClick="goLastWeek(<?php echo $day . "," . $month;?>);" >
</body>
</html>
greetings bmwlaval, , I do not see that you are using the javascript Date object, as the date operator for your "change Date" operation? ? I don't see what you are doing. As far as I have seen in javascript the months are 0 initiated, as in January is 0 not 1 .
I would create a begining Reference date,
var dateObj = new Date("March 24, 2011");
in PHP I think that date might be written with - $date2=date("F j Y");
but you might check it because I do not have access to PHP at this time;
or use a javascript date created with just numbers, remember 2 is for the third month March
var dateObj = new Date(11,2,24);
then you can use that dateObj to change the date
dateObj.setDate(dateObj.ge tDate()-7) ; // this is a week before, subtract 7 days
then use something like this to send the date -
document.location.href = "cal.php?date="+dateObj.ge tMonth()+" -"+dateObj .getDate() +"-"+dateO bj.getFull Year();
then in PHP you might use something like this
$date = $_GET['date'];
if (strlen($date)>6) {$date = explode('-', $date);}
this is all untested code, but I hope it gives you an Idea of how you might try to do dis
Ask questions, if my explanation was not clear. . .
I would create a begining Reference date,
var dateObj = new Date("March 24, 2011");
in PHP I think that date might be written with - $date2=date("F j Y");
but you might check it because I do not have access to PHP at this time;
or use a javascript date created with just numbers, remember 2 is for the third month March
var dateObj = new Date(11,2,24);
then you can use that dateObj to change the date
dateObj.setDate(dateObj.ge
then use something like this to send the date -
document.location.href = "cal.php?date="+dateObj.ge
then in PHP you might use something like this
$date = $_GET['date'];
if (strlen($date)>6) {$date = explode('-', $date);}
this is all untested code, but I hope it gives you an Idea of how you might try to do dis
Ask questions, if my explanation was not clear. . .
sorry, you use the javascript and write it with PHP like this
var dateObj = new Date("<?php echo date("F j Y") ?>");
not tested, might have syntax errors
var dateObj = new Date("<?php echo date("F j Y") ?>");
not tested, might have syntax errors
ASKER
Sorry for the delayed response.
I didn't realise that javascript had date functionality. You're suggesting that I set the date using
var dateObj = new Date("<?php echo date("F j Y") ?>");
But what if I am passing the date from the original code through the function and want to use that date in my goLastWeek(day,month,year) function. I don't really want the current date being used in the function because if the user clicks the button to go back a week several time it needs to always recalculate the date based on the week that is currently being used.
For ex:
Todays Date
Button Click
Go back 7 days
Button Click
Go back 7 days...etc
How would I manipulate the numbers that I am receving, but also use the javascript date functionality?
I didn't realise that javascript had date functionality. You're suggesting that I set the date using
var dateObj = new Date("<?php echo date("F j Y") ?>");
But what if I am passing the date from the original code through the function and want to use that date in my goLastWeek(day,month,year)
For ex:
Todays Date
Button Click
Go back 7 days
Button Click
Go back 7 days...etc
How would I manipulate the numbers that I am receving, but also use the javascript date functionality?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
I've determined that this whole process would be easier using PHP.
The solution will be to do the date calculation in javascript,
you could use this javascript library to get the same functionality (http://phpjs.org/functions/strtotime:554).