Jump to content

Some basic questions about PHP to talk about...Please.


makuc

Recommended Posts

Don't bother. I figure out that I have to change else to else if . Thanks anyway. Now my clock is completely finished. The only thing I might integrate later on is external configuration file for personalizing how clock is shown (you can change the sequence to match your way of view - where is shown: day, where month, year etc.).

 

And my latest clock even know when February have 28 and when 29 days :D . I have been bothering my head with that for very long time :).

You can preview it at: http:cybital.cu.cc/clock.php

Link to comment
Share on other sites

Since my clock is finished (in case I won't integrate external configuration file), I decided that it would be good if an expert would look at my code, since this is my first "program" (script) xD.

So, here it is:

<?php
$seconds = date(s);
$minutes = date(i);
$hours = date(H);
$dayweek = date(w);
$day = date(d);
$month = date(m);
$year = date(Y);
$dayadd = date(S);
?>
<script type="text/javascript">
<!--
/**********************************************************
*****|_______________________________________________|*****
*****|...............................................|*****
*****|............Cybital Inc. Production............|*****
*****|...............................................|*****
*****|................Proudly presents:..............|*****
*****|....PHP & JavaScript Real-Time Server Clock....|*****
*****|...............................................|*****
*****|...The only JS and PHP Real-Time Server Clock..|*****
*****|...without any use of AJAX which would cause...|*****
*****|..additional server overloading while it would.|*****
*****|.......keep requesting time from server........|*****
*****|.So here it is, Real-Time Server Clock with NO.|*****
*****|..........addition server overloading..........|*****
*****|...............................................|*****
*****|.......Copyright© Cybital Inc. 2011-<?php echo date(Y);?>.......|*****
*****|_______________________________________________|*****
**********************************************************/
var s=<?php echo($seconds);?>;
var m=<?php echo($minutes);?>;
var h=<?php echo($hours);?>;
var day=<?php echo($day);?>;
var dayWeek=<?php echo($dayweek);?>;
var month=<?php echo($month);?>;
var year=<?php echo($year);?>;
var nextLeapYear=2000;
var currentLeapYear=nextLeapYear;

function clock(){
s=s+1;
if(s==60){
	s=0;
	m=m+1;
}
if(m==60){
	m=0;
	h=h+1;
}
if(h==24){
	h=0;
	dayWeek=dayWeek+1;
	day=day+1;
}
if(dayWeek==7){
	dayWeek=0;
}
numDaysPerMonth();
dayNaming();
dayDisplay();
monthNaming();
betterSec();
betterMin();

document.getElementById('Clock').innerHTML="<PRE>"+h+":"+betterMinutes+":"+betterSeconds+" | "+dayName+" - "+day+dayadd+" "+monthName+","+year+"</PRE>";
setTimeout("clock()",1000);
}

function dayNaming(){
if(dayWeek==0){
	dayName="Sunday";
}
if(dayWeek==1){
	dayName="Monday";
}
if(dayWeek==2){
	dayName="Tuesday";
}
if(dayWeek==3){
	dayName="Wednesday";
}
if(dayWeek==4){
	dayName="Thursday";
}
if(dayWeek==5){
	dayName="Friday";
}
if(dayWeek==6){
	dayName="Saturday";
}
}
function dayDisplay(){
if(day==1){
	dayadd="st";
}
if(day==2){
	dayadd="nd";
}
if(day==3){
	dayadd="rd";
}
else{
	dayadd="th";
}
}
function monthNaming(){
if(month==1){
	monthName="January"
}
if(month==2){
	monthName="February"
}
if(month==3){
	monthName="March"
}
if(month==4){
	monthName="April"
}
if(month==5){
	monthName="May"
}
if(month==6){
	monthName="June"
}
if(month==7){
	monthName="July"
}
if(month=={
	monthName="August"
}
if(month==9){
	monthName="September"
}
if(month==10){
	monthName="Oktober"
}
if(month==11){
	monthName="November"
}
if(month==12){
	monthName="December"
}
}
function betterSec(){
if(s<=9){
	betterSeconds="0"+s;
}
else{
	betterSeconds=s;
}
}
function betterMin(){
if(m<=9){
	betterMinutes="0"+m;
}
else{
	betterMinutes=m;
}
}

function numDaysPerMonth(){
if(month==1){
	if(day==32){
		day=1;
		month=month+1
	}
}

if(month==2){
	if(nextLeapYear<year){
		nextLeapYear=nextLeapYear+4
	}
	if(nextLeapYear==year){
		if(day==30){
			day=1;
			month=month+1;
		}
	}
	else if(day==29){
		day=1;
		month=month+1
	}
}

if(month==3){
	if(day==32){
		day=1;
		month=month+1
	}
}
if(month==4){
	if(day==31){
		day=1;
		month=month+1
	}
}
if(month==5){
	if(day==32){
		day=1;
		month=month+1
	}
}
if(month==6){
	if(day==31){
		day=1;
		month=month+1
	}
}
if(month==7){
	if(day==32){
		day=1;
		month=month+1
	}
}
if(month=={
	if(day==32){
		day=1;
		month=month+1
	}
}
if(month==9){
	if(day==31){
		day=1;
		month=month+1
	}
}
if(month==10){
	if(day==32){
		day=1;
		month=month+1
	}
}
if(month==11){
	if(day==31){
		day=1;
		month=month+1
	}
}
if(month==12){
	if(day==32){
		day=1;
		month=month+1
	}
}
}
--></script>
<meta charset="iso-8859-1" />
<div>Current server time is:<br />
<div id="Clock">Here should be shown Server Clock! Check if you have JavaScript enabled!</p>
<script type="text/javascript">
clock();
</script>

So, that's all. Any help would be appreciated.

Link to comment
Share on other sites

It's fine for a first script but it has a lot of flaws. Before you start writing software for someone else to use, try to write some software for your own and re-use it on multiple projects. You'll start to notice the flaws in your script.

 

Some pointers:

 

1. Use of global variables in a function

2. Bad variable/function naming

3. Strongly coupled to specific HTML

4. Hard to test

Link to comment
Share on other sites

What do you mean by bad naming?  :confused:  Could you give me some examples (because this is naming I am used to, even from naming my documents files on my computer)?

And could you, please, post some improvements to the code (perhaps just a piece of code) to show me what exactly do you mean? Any advice (or even a little more explained) is very welcome :D .

Link to comment
Share on other sites

dayNaming();
dayDisplay();
monthNaming();
betterSec();
betterMin();

 

This is what I mean with bad function naming. Everybody would have to make a wild guess as to what these functions do or read through it's function body while instead they should be just black boxes.

 

dayNaming() would be better if you called it something along the lines of convertWeekDayToFullName() as that describes it's behavior and pass the weekDay in instead of just grabbing it from the global namespace, I advice you to do this for all functions using global variables.

 

You also have duplicate code betterMin() and betterSec() do the same thing, generalize the function and give it a proper name.

Link to comment
Share on other sites

Heh, finally back here :) . So i have a question. this time about HTML and CSS =P .

I want to build a menu, which have for links text, not images, so that it would depend on the language what it should show ;D Of course I know how to make that, otherwise I wouldn't even start :) But there is a little problem I am facing now. How could I bring text in <a> tags like 5px higher than it's current position? Because if I use <div>, it start breaking my whole design, if it is not live preview (i am using dreamweaver cs5.5 for previewing purposes =P ). Is there any other way to to make the text appear a littl ehigher? Thanks.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.