Forum Moderators: coopster

Message Too Old, No Replies

Custom PhP Date Script Help

         

42yen

5:51 pm on Apr 6, 2010 (gmt 0)

10+ Year Member



I am looking for assistance in creating a php date script that will display the following:

Today is April 06, A.S. XLIV (44)

I have created a simple date script that uses server time, made adjustments for time zone, etc, but two things are really beyond my php skills.

1) The "calendar" system starts May 1, 1966. So 1966 equal year I (1), 1967 is II (2) and now 2010 is XLIV (44) etc...

2) The year is displayed in roman numerals.

Any and all help would be greatly appreciated.

Readie

6:22 pm on Apr 6, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



For the year

$now_year = (date(Y) - 1965);

To apply Roman numeral formatting, try this: [pear.php.net...]

---

require_once 'Numbers/Roman.php';
$now_full = date(F d) . ', A.S. ' . Numbers_Roman::toNumeral((date(Y) - 1965)) . ' (' . (date(Y) - 1965) . ')';
echo $now_full;

42yen

6:41 pm on Apr 6, 2010 (gmt 0)

10+ Year Member



I was afraid the year was that simple. =)

Now how would I make the year start on May 1st instead of January 1st?

Readie

7:18 pm on Apr 6, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Now that's a little more complicated, but doable :)

$now_month = date(n);
$now_month = $now_month + 4;
if($now_month > 12) {
$now_month = $now_month - 12;
}
echo date(F, mktime(0, 0, 0, $now_month));

42yen

8:10 pm on Apr 6, 2010 (gmt 0)

10+ Year Member



a cup of coffee and I think I am way over my head...

this is what I have so far (have not even tried the roman numerals yet):

===

<?php
$timezone = new DateTimeZone( "America/Los_Angeles" );
$date = new DateTime();
$date->setTimezone( $timezone );
$now_year = (date(Y) - 1965);
$now_month = date(n);
$now_month = $now_month + 4;
if($now_month > 12) {
$now_month = $now_month - 12;
}
echo date(F, mktime(0, 0, 0, $now_month));
?>

===

For some reason it returns "August". I am going back line by line trying to figure out where I monkeyed it up. Not sure where you are, or if you do this for a living or not, but I owe you a pint!

Matthew1980

8:24 pm on Apr 6, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi there 42yen,

Welcome to the forum if these are your first posts :)

When you echo the date at the end of the script there, what exactly is on the screen, is it just August? or is there anything else?

Cheers,
MRb

42yen

8:26 pm on Apr 6, 2010 (gmt 0)

10+ Year Member



Yes, it simply returns


August


In the top left corner of the browser. As this is my first attempt at php I am at a loss as to what to do to correct it. The famous saying "It looked better in my head" comes to mind...haha

42yen

8:29 pm on Apr 6, 2010 (gmt 0)

10+ Year Member



Ah, I do not have the Y/m/d formating in there (have yet to check it). Well, I do have the month, (F) in the script above, but not the rest.

Readie

8:31 pm on Apr 6, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You need to incorporate my second post into my first post :)

$now_month = date(n);
$now_month = $now_month + 4;
if($now_month > 12) {
$now_month = $now_month - 12;
}
$now_month = date(F, mktime(0, 0, 0, $now_month));

require_once 'Numbers/Roman.php';
$now_full = $now_month . ' ' . date(d) . ', A.S. ' . Numbers_Roman::toNumeral((date(Y) - 1965)) . ' (' . (date(Y) - 1965) . ')';
echo $now_full;

Matthew1980

8:42 pm on Apr 6, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hey Readie,

You beat me at typing tonight ;) But I am injured...

Would strtotime(); do the same thing here, as I think that could possibly be easier to start the year on, as you could literally tell the function to start on "May".

Don't forget to enclose the date formatting chars in "", otherwise you will flag up undefined constant errors, just thought I should mention it there ;-p

Cheers,
MRb

42yen

8:54 pm on Apr 6, 2010 (gmt 0)

10+ Year Member



OK, so the script should look like this:

===

<?php
$timezone = new DateTimeZone( "America/Los_Angeles" );
$date = new DateTime();
$date->setTimezone( $timezone );
$now_year = (date(Y) - 1965);
$now_month = date(n);
$now_month = $now_month + 4;
if($now_month > 12) {
$now_month = date(n);
$now_month = $now_month + 4;
if($now_month > 12) {
$now_month = $now_month - 12;
}
$now_month = date(F, mktime(0, 0, 0, $now_month));

require_once 'Numbers/Roman.php';
$now_full = $now_month . ' ' . date(d) . ', A.S. ' . Numbers_Roman::toNumeral((date(Y) - 1965)) . ' (' . (date(Y) - 1965) . ')';
echo $now_full;
?>

===

I have yet to download/install the roman numeral package. Just to show I was doing some research in trying to figure this out I did find:

[go4expert.com...]

As for the rest... Wow. I should of stuck to the excitement of the wee little "Hello World" script I did followed by the today is <date> one.

I think I jumped the gun big time on this one, but am glad I found the site and extremely grateful for the help!

Matthew1980

9:19 pm on Apr 6, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi there 42yen,

Don't forget to put the "" on the date formatting chars:-

<?php
$timezone = new DateTimeZone( "America/Los_Angeles" );
$date = new DateTime();
$date->setTimezone( $timezone );
$now_year = (date("Y") - 1965);
$now_month = date("n");
$now_month = $now_month + 4;
if($now_month > 12) {
$now_month = date("n");
$now_month = $now_month + 4;
if($now_month > 12) {
$now_month = $now_month - 12;
}
$now_month = date("F", mktime(0, 0, 0, $now_month));

require_once 'Numbers/Roman.php';
$now_full = $now_month . ' ' . date("d") . ', A.S. ' . Numbers_Roman::toNumeral((date("Y") - 1965)) . ' (' . (date("Y") - 1965) . ')';
echo $now_full;
?>

Only reason I point this out is because not entering them properly will more than likely flag up a undefined constant error, and this would place a warning message across the top of your browser.

Don't worry about how fast or slowly you learn/pick up things, this is the best way to do this sort of thing, take a working example, and tweak it to see how you affect the running or format. Yes though, the same can be said for any language when you set out to learn it, the first steps are the hardest, stick with it though, as in a few weeks you will look on this and think, wow, easy really!

But even accomplished folk still make schoolboy errors :)

Good luck anyway,

Cheers,
MRb

42yen

9:20 pm on Apr 6, 2010 (gmt 0)

10+ Year Member



Two last question and I will leave you alone before I owe you both an entire pub full of pints...

1) I am currently running (well testing) the date.php from my main web directory is it safe to keep it there once I am set to call it from inside another .html page

2) Install of the Roman Numerals package - Is that safe to place in the main directory as well?

Cheers!

Readie

9:21 pm on Apr 6, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



if($now_month > 12) {
$now_month = date(n);
$now_month = $now_month + 4;

That'd be unnecessary there :)

---

>> Matt
Injured? Takes the fun out of winning then :P
Hope it heals up soon :)

I've never used strtotime so I shall have to read up on it

>> 42yen
Hehe, my first project in PHP? Seperating two sites at a code level rather than a file level - I had to come here for help too :)

[EDIT] Now I'm slow typing :)

WRT date("Y") etc... - You only need quotes around non-date things

date(Ymd) works
date(Y-m-d) Does not
date("Y-m-d") works

---

It's safe to keep them both there... Although I would make another directory to place them both in myself.

42yen

9:25 pm on Apr 6, 2010 (gmt 0)

10+ Year Member



You guys rock! I'm about to run out the door for a bit, but with the edits suggested so far this is the final script, correct?

===

<?php
$timezone = new DateTimeZone( "America/Los_Angeles" );
$date = new DateTime();
$date->setTimezone( $timezone );
$now_year = (date("Y") - 1965);
$now_month = date("n");
$now_month = $now_month + 4;
if($now_month > 12) {
$now_month = $now_month - 12;
}
$now_month = date("F", mktime(0, 0, 0, $now_month));

require_once 'Numbers/Roman.php';
$now_full = $now_month . ' ' . date("d") . ', A.S. ' . Numbers_Roman::toNumeral((date("Y") - 1965)) . ' (' . (date("Y") - 1965) . ')';
echo $now_full;
?>

===

To continue my education on the matter I'll give myself the home work of going back and making comments to each line so I can learn, understand and remember what each one does.

Matthew1980

9:38 pm on Apr 6, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi there 42yen,

1) I am currently running (well testing) the date.php from my main web directory is it safe to keep it there once I am set to call it from inside another .html page


Eek! Don't give it the extension of .html if you are going to call the file from inside it, especially if you are using php to include the file, use the extension .php.

Even if the only php content in the file is to echo the date, always call the file .php else the parser won't recognise that it has to 'interpret' the data before it gets displayed by the browser, and you will just end up with the code printed to screen as this is what the browser will think is the correct course of action.

Just as habit now, I only call the files .html (or .htm) using mod_rewrite, but that's another day another lesson ;-p

Readie:-

Nothing some rest wouldn't cure :)

WRT date("Y") etc... - You only need quotes around non-date things


use error_reporting(E_ALL); to see what I mean :)


Cheers,
MRb

Readie

9:57 pm on Apr 6, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



always call the file .php else the parser won't recognise that it has to 'interpret' the data before it gets displayed by the browser

Not 100% true... You can set Apache to parse php inside a .html file. I don't advise it, but you can do it.

use error_reporting(E_ALL); to see what I mean :)

Ahh, I do see :)

42yen

10:19 pm on Apr 6, 2010 (gmt 0)

10+ Year Member



Woot! It *almost* works. Still getting the month of August, but thanks to all of your work it's almost there. I assume I just need to play with the +/- on the month to get it to work. And with May just around the corner it will be easy to see how that one works as well.

Readie

10:24 pm on Apr 6, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You're getting August because it's currently April :)
In January it'll say April

42yen

10:33 pm on Apr 6, 2010 (gmt 0)

10+ Year Member



OK here she is:

===

<?php
$timezone = new DateTimeZone( "America/Los_Angeles" );
$date = new DateTime();
$date->setTimezone( $timezone );
$now_year = (date("Y") - 1966);
$now_month = date("n");
$now_month = $now_month + 0;
if($now_month > 12) {
$now_month = $now_month - 12;
}
$now_month = date("F", mktime(0, 0, 0, $now_month));

require_once 'Roman.php';
$now_full = $now_month . ' ' . date("d") . ', A.S. ' . Numbers_Roman::toNumeral((date("Y") - 1965)) . ' (' . (date("Y") - 1965) . ')';
echo $now_full;
?>

===

I changed $now_month + 0 so it shows the correct month and it currently shows April. However, I have this odd feeling that this will bite me in the near future

I changed date("Y") - 1966 to both a year ealier and later to get the correct A.S. date yet it remains on A.S. XLV (45). Is this because I changed the month +?

The script in action here [woodstock-manor.com]. And the calendar system it was based of here [sca.org].

42yen

10:38 pm on Apr 6, 2010 (gmt 0)

10+ Year Member



found the other - 1965's and changed them. The date(s) now show correctly. Hard to believe I can miss so much in such a short script. Thanks again!

Readie

10:43 pm on Apr 6, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



$now_month = $now_month + 0;
if($now_month > 12) {
$now_month = $now_month - 12;
}

All unnecessary.

The + 4 and - 1965 were included because I got the impression that's what you were after in your original posts :)

42yen

10:45 pm on Apr 6, 2010 (gmt 0)

10+ Year Member



@Readie I do want the "year" to start in May. Is that what those lines do?

For example May 1st of this year would be:

May 01, A.S. XLV (45)

Readie

10:55 pm on Apr 6, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



When they added 4 to the month, and took away 1965 from year they were equating the current date to (bear with me here)

4 months in the future
1965 years in the past

So if today was January 1st, 1966, that code would display
May 01, A.S. I (1)

---

I have just realised a slight hitch with this script however, you could end up getting things like February 30th.

To fix that would require quite a bit more coding.

42yen

11:06 pm on Apr 6, 2010 (gmt 0)

10+ Year Member




When they added 4 to the month, and took away 1965 from year they were equating the current date to (bear with me here)

4 months in the future
1965 years in the past

So if today was January 1st, 1966, that code would display
May 01, A.S. I (1)


So then when May 1st rolls around it will return the date as May 01, A.S. XLV (45) even if I removed the lines you mentioned above?

I'm going line by line now with php.net to understand the code you have written.

42yen

11:09 pm on Apr 6, 2010 (gmt 0)

10+ Year Member



For Leap years then would I need to work in something from here:

[php.net...]

or is it much more complicated?

Readie

11:10 pm on Apr 6, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



When May 01 rolls around, it would say September - as that is 4 months later :)

42yen

11:19 pm on Apr 6, 2010 (gmt 0)

10+ Year Member



Even if I set +4 to +0 like in the example posted above?

42yen

11:22 pm on Apr 6, 2010 (gmt 0)

10+ Year Member



O' just found this:

Year:
L: 1 if it's a leap year and 0 if it isn't.
Y: A four digit year format
y: A two digit year format. Values 00 through 99.

From here:
[tizag.com...]

The example they give (for full time/date display)is:

("D, d M Y H:i:s O")

Readie

11:26 pm on Apr 6, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



If you set it to + 0, then
$now_month = $now_month + 0;
if($now_month > 12) {
$now_month = $now_month - 12;
}
All starts doing nothing, and can be deleted. And the current month will be shown.

I'm sorry, but you are confusing me a bit with what you want here, as first you seem to want a date system that's out of sync with the current one, and then you seem to want one that's in sync :/
This 32 message thread spans 2 pages: 32