Solved

Subtracting time using JS function and PHP date functions

Posted on 2011-03-24
10
437 Views
Last Modified: 2012-05-11
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!
<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>

Open in new window

0
Comment
Question by:bmwlaval
  • 5
  • 3
  • 2
10 Comments
 
LVL 8

Expert Comment

by:Rik-Legger
ID: 35208957
This is not gonne work becuase your php code is only parsed once and your expecting it to be parsed everytime the javascript function will be executed. Just not gonne work.

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).
0
 
LVL 1

Author Comment

by:bmwlaval
ID: 35209122
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?
0
 
LVL 8

Expert Comment

by:Rik-Legger
ID: 35209142
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.
0
 
LVL 1

Author Comment

by:bmwlaval
ID: 35209649
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?
0
 
LVL 1

Author Comment

by:bmwlaval
ID: 35210784
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.
<!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>

Open in new window

0
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
LVL 33

Expert Comment

by:Slick812
ID: 35211994
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.getDate()-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.getMonth()+"-"+dateObj.getDate()+"-"+dateObj.getFullYear();


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. . .
0
 
LVL 33

Expert Comment

by:Slick812
ID: 35212019
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
0
 
LVL 1

Author Comment

by:bmwlaval
ID: 35241162
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?

0
 
LVL 33

Accepted Solution

by:
Slick812 earned 250 total points
ID: 35243515
I gave some suggestions using the javascript DateObject, I do not know anything about how or why you want or need to set up your methods for the input and output you may need. In your code you posted you have -
newday = <?php echo date("j",day);

so I gave an example using PHP date to javascript DateObject,
You really should make you a test page in html to use on your computer, and make several functions in javascript using the Date Object, so you can use it and see how it works. Here is one reference page for the javascript Date Object -
http://www.w3schools.com/js/js_obj_date.asp
   and
http://www.comptechdoc.org/independent/web/cgi/javamanual/javadate.html

as for your request - "How would I manipulate the numbers that I am receiving, but also use the javascript date functionality?"

I do not know anything about the "numbers that you am receiving", so I can't comment on that directly, however if you use the code I gave -
  document.location.href = "cal.php?date="+dateObj.getMonth()+"-"+dateObj.getDate()+"-"+dateObj.getFullYear();

and pass those numbers back -
2-25-2011

then you might use what I have allready showed you
var dateObj = new Date(2011,2,25);
see the php2jsDate( ) function below, ,
I have posted some javascript code below to get you started with the date object, but you will need to learn to use it for the Date information that you may need. It is untested code, but may give you some Ideas on how the Date Object can be used.

ALSO, please keep in mind that your PHP server time may not be the browsers Time Zone, it may be Monday on your server time and Tuesday on the clients browser time, because they are in a different parts of the globe.

I hope this helps.
<script language="JavaScript">

function php2jsDate(phpString){
// phpString = "2-25-2011";
dArray= phpString.split('-');
var dateObj1 = new Date(dArray[2],dArray[0],dArray[1]);
return dateObj1;
}

function setLastWeek(dateObj){
dateObj.setDate(dateObj.getDate()-7);
return dateObj;
}

function getClockTime(dm) {
var now=new Date();
var hour=now.getHours();
var minute=now.getMinutes()+1;
var ap="am";
if(hour>11){ap="pm";}
if(hour>12){hour=hour -12;}
if(hour==0){hour=12;}
if(minute<10){minute="0"+minute;}
if(dm>1){return hour+':'+minute+' '+ap;}
  else
  {return hour+':'+minute+'.'+now.getSeconds()+' '+ap;}
}

</script>

Open in new window

0
 
LVL 1

Author Closing Comment

by:bmwlaval
ID: 35282872
I've determined that this whole process would be easier using PHP.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Someone recently asked me about how to display a progress indicator on a page while an iframe is loading. And I remember when I first came across this myself. It was a bit tricky to get my head around, but really, it's very simple. The most impor…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
In this tutorial viewers will learn how to position overlapping items using z-index in CSS. They will also learn the restrictions on the z-index property.  Create a new HTML document with an internal stylesheet.: Create a div in CSS and name it Red.…
The viewer will learn how to dynamically set the form action using jQuery.

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now