Be a Supporter!
Jams44
Jams44
  • Member since: Nov. 8, 2004
  • Offline.
Forum Stats
Member
Level 07
Blank Slate
Php: Date Displayer 2005-12-15 22:58:02 Reply

PHP: Main

A Simple Date Displayer

Probably one of the easier script, this code displays the current date in words (Months, Days) rather than numbers. So let's begin with the good old PHP opening tag

<?

Next we will define all the days, starting with Sunday (which I think is technically the first day of the week rather than Monday according to the church).

$dayname[0] = "Sunday";
$dayname[1] = "Monday";
$dayname[2] = "Tuesday";
$dayname[3] = "Wednesday";
$dayname[4] = "Thursday";
$dayname[5] = "Friday";
$dayname[6] = "Saturday";

Define the months...

$monthname[1] = "January";
$monthname[2] = "February";
$monthname[3] = "March";
$monthname[4] = "April";
$monthname[5] = "May";
$monthname[6] = "June";
$monthname[7] = "July";
$monthname[8] = "August";
$monthname[9] = "September";
$monthname[10] = "October";
$monthname[11] = "November";
$monthname[12] = "December";

Next we will define the datenumber using the date function

$daynum = date("w");

Same thing but with months...

$monthnum = date("n");

And years...

$year = date("Y");

And good old day (number)

$day = date("d");

Now we will correspond all the the days with the order they come in. (ex. Tuesday[2])

$textday = $dayname[$daynum];

and with the months... not much explaining in this tutorial, who said PHP was difficult lol? XD

$month = $monthname[$monthnum];

Finally lets find out the date by echoing it in this order

echo "Todays Day Is: $textday, $day $month $year";

Thanx for viewing this tutorial which was made by Jams44 for PHP: Main

Enjoy and keep reading!

?>

cook
cook
  • Member since: Mar. 21, 2004
  • Offline.
Forum Stats
Member
Level 14
Blank Slate
Response to Php: Date Displayer 2005-12-15 23:16:06 Reply

Much simpler (Posted without offence) =)

<?
echo "Todays date is: ".strftime("%A, %d - %Y",time());
?>


"Days of planning. Weeks of building. Months of perfecting. Seconds of smashing."

BBS Signature
cook
cook
  • Member since: Mar. 21, 2004
  • Offline.
Forum Stats
Member
Level 14
Blank Slate
Response to Php: Date Displayer 2005-12-15 23:17:23 Reply

At 12/15/05 11:16 PM, -cook- wrote: Much simpler (Posted without offence) =)

<?
echo "Todays date is: ".strftime("%A, %d - %Y",time());
?>

Oops, forgot to put the month in there.

http://ca.php.net/strftime


"Days of planning. Weeks of building. Months of perfecting. Seconds of smashing."

BBS Signature
Craige
Craige
  • Member since: Jul. 17, 2004
  • Offline.
Forum Stats
Member
Level 08
Blank Slate
Response to Php: Date Displayer 2005-12-15 23:17:51 Reply

To needlessly complex. You can achive the same thing with one line:
date('l, j F Y');

IWantSomeCookies
IWantSomeCookies
  • Member since: Aug. 20, 2004
  • Offline.
Forum Stats
Member
Level 13
Blank Slate
Response to Php: Date Displayer 2005-12-16 05:06:49 Reply

Heh, 3 great ways of doing it. Thanks guys.. :-)


"Actually, the server timed out trying to remove all your posts..."
-TomFulp

EviLDoG
EviLDoG
  • Member since: Oct. 18, 2000
  • Offline.
Forum Stats
Member
Level 12
Blank Slate
Response to Php: Date Displayer 2005-12-16 07:00:01 Reply

You can also make daily crons, e.g:
<?php
// the file that will be checked to see if the day has changed
$filename = 'daily_cron.db.php';
// file exists by default, change to false later on if it don't exist
$exists = TRUE;
if (!file_exists ($filename)) {
echo 'File '.$filename.' does not exist!';
echo 'Attempting to create file...';
// fopen () returns false and outputs an error if it hasn't worked
if ($fp = fopen ($filename, 'w+')) {
fclose ($fp);
echo 'File successfully created!';
$created = TRUE;
} else {
$exists = FALSE;
// check to see if the filename used has a path already entered,
// if not use the current path in the error
// Uses a one line if: $var = ( statement ) ? TRUE : FALSE;
// If the statement is true, $var is assigned with the info entered in between
// the question mark and the semi colon, if false, the info after the semi colon is used
$curdir = (strstr ('/', $filename) || strstr ('\\', $filename) ) ? dirname ($filename) : dirname($HTTP_SERVER_VARS['PHP_SELF']);
echo 'Could not create file, please upload the file '.$filename.' to the '.$curdir.' directory and chmod it to 666';
}
}

if ($exists) {
// When no 2nd arguments are entered in date() it uses the current time
// fimemtime(filetime) gets the unix timestamp of when the file was last modified
if ( date ('Y-m-d', filemtime ($filename) ) != date ('Y-m-d') || $created ) {
# put all the stuff you want to be in the daily cron here.
// Update the last modified time of the file
$fp = fopen ($filename, 'w');
fclose ($filename);
}
}
?>

Again i haven't checked the code entirely, but this is handy for creating a new user of the day and such, you can also set it to hours and stuff by changing the date arguments.
Just put that code on a popular page on your site, or a page you know will get visited every day.

Greeley
Greeley
  • Member since: Aug. 30, 2005
  • Offline.
Forum Stats
Member
Level 13
Blank Slate
Response to Php: Date Displayer 2005-12-17 20:38:28 Reply

Dezmerkts way is way easier if you are wanting to just simply display the date but for some projects and maybe in some cases what Jams has taught us might be useful.

NinoGrounds
NinoGrounds
  • Member since: Nov. 28, 2005
  • Offline.
Forum Stats
Member
Level 19
Programmer
Response to Php: Date Displayer 2006-01-10 15:01:52 Reply

date() will do the job, this is way to complex.

Sir-Davey
Sir-Davey
  • Member since: Jul. 9, 2001
  • Offline.
Forum Stats
Member
Level 19
Blank Slate
Response to Php: Date Displayer 2006-01-11 06:24:39 Reply

This is like doing
<?
mysql_connect('localhost','myUser','myPass
');
mysql_select_db('myDatabase');
mysql_query('
CREATE TABLE hello_chars(
'char' VARCHAR(1) NOT NULL
)
');
$text = 'Hello world!';
$res = Array();
for ( $i = 0; $i < strlen($arr); $i++ )
{
$res[] = $text{$i};
}
foreach ( $res as $value )
{
mysql_query("INSERT INTO hello_chars VALUES('" . $value . "')");
}
$arr2 = mysql_fetch_array($mysql_query('SELECT * FROM hello_chars'));
for ( $i = 0; $i < count($arr2); $i++ )
{
echo $arr2[$i];
}
?>
Didn't test, but it should work!
?>


BBS Signature
Pilot-Doofy
Pilot-Doofy
  • Member since: Sep. 13, 2003
  • Offline.
Forum Stats
Member
Level 37
Musician
Response to Php: Date Displayer 2006-01-11 12:40:22 Reply

At 1/10/06 03:01 PM, Nino_JoJ wrote: date() will do the job, this is way to complex.

I agree. There's no point in manually typing the days out unless you're making a date selection menu for birthdays, etc. Then you can also use a for loop to count back in time or ahead in time to retrieve the days, months, and so forth..