Permalänk

Ett datum skript.

Fick för mig att skriva detta datumskript idag.

Jag undrar hurfan det kan förbättras.

Samt hur är det man skriver den förste, andre, tredje, fjärde, femte, etc. fast 1st, 2nd, 3rd, 4th, 5ft, etc.

<?php /* Instructions: To use this on some page just use the include function or copy all this into the page. The following should be inserted inside a div tag in body. <?php echo ($day." the ".$daynr." of ".$month." year ".$year); ?> This could be changed so it would be correct to your language. The month function. You may change this to your own language. It's curently written in Swedish. But could be written in any of the existing languages that use the gregorian calendar. */ $month = (array( 01 => "Januari", 02 => "Februari", 03 => "Mars", 04 => "April", 05 => "Maj", 06 => "Juni", 07 => "Juli", 08 => "Augusti", 09 => "September", 10 => "Oktober", 11 => "November", 12 => "December" )); /* The day function. You may change this to your langugage. */ $day = (array( Mon => "Måndag", Tue => "Tisdag", Wed => "Onsdag", Thu => "Torsdag", Fri => "Fredag", Sat => "Lördag", Sun => "Söndag" )); /* The day number funtion. You may change this to your language. */ $daynr = (array( 01 => "1", 02 => "2", 03 => "3", 04 => "4", 05 => "5", 06 => "6", 07 => "7", 08 => "8", 09 => "9", 10 => "10", 11 => "11", 12 => "12", 13 => "13", 14 => "14", 15 => "15", 16 => "16", 17 => "17", 18 => "18", 19 => "19", 20 => "20", 21 => "21", 22 => "22", 23 => "23", 24 => "24", 25 => "25", 26 => "26", 27 => "27", 28 => "28", 29 => "29", 30 => "30", 31 => "31" )); $month = $month[date("m")]; $day = $day[date("D")]; $daynr = $daynr[date("d")]; $year = date("Y"); echo ($day." den ".$daynr." ".$month." år ".$year); ?>

Permalänk
Medlem

<?php setlocale(LC_TIME, 'sv_SE'); echo strftime("%Aen den %d %B år %G"); ?>

EDIT: Det är jävligt mycket kortare och enklare i alla fall. Även om det kanske inte var den efterfrågade förbättringen av ditt skript. Hemligheten med PHP är att alltid kolla om det redan finns en funktion för det.

EDIT2: Grejade lite tajpos och skit.

EDIT3: Kolla, automatiskt enligt våra nationella standarder!

<?php echo strftime("%c"); ?>

Glöm inte setlocale() innan bara.

Visa signatur

Brass knuckles and a 2x4

Permalänk

Jo kortare blev den helt klart. Men jag finner min version lite coolare.

Permalänk
Medlem

Skrev en datumklass i C# inspirerad av php motsvarande funktion, du kan ju kolla om du hittar något av värde...

/* * Date class based on unix Epoch time stamp, second from 00:00:00 1/1 1970 * * beta 1 * ** setDate(long second, long minute, long hour, long day, long month, long year) * Sets Date variables can be overloaded, e.g. you can set 25 hours or -23 minutes //month don't work as negativ yet * year: both full (4 digits) and short (2 digits) supported, years > 100 counts as full, years < 70 counts as years + 2000, years >= 70 and <= 100 counts as years + 1900 * ** addDate(long second, long minute, long hour, long day, long month, long year) * Add Date value, same as above... * ** setUDate(long unixStamp) * Sets the unix time stamp directly * ** addUDate(long unixStamp) * Adds seconds to time stamp value * ** setTimeZone(float timeZoneHour) ***Används ej i version * Sets time zone as float of hours from +0000 * ** setTimeZoneSec(long timeZone) ***Används ej i version * Sets time zone as int of seconds from +0 * ** string getDate(string referens) * return string of wanted type, free formating, unknown chars passes through to string (very similar to the date() function in php) * * d : Day of the month, 2 digits with leading zeros, 01 to 31 * j : Day of the month, without leading zeros, 1 to 31 * S : English ordinal suffix for the day of the month, 2 characters, st for 1, nd for 2 and so on (works well with j) * D : Day of the week, 3 letters, Mon to Sun * l : Day of the week, full text, Monday to Sunday * w : Day of the week, number, 0 (monday) to 6 (sunday) * z : Day of the year, number, 0 (1 jan) to 364 (365 if leap) * W : Week of the year, number, starting on 1 * F : Month, full text, January to December * M : Month, 3 letters, Jan to Dec * m : Month, 2 digits with leading zeros, 01 to 12 * n : Month, without leading zeros, 1 to 12 * t : Days in current month, 28 to 31 * L : returns 1 if leap year, else 0 * Y : Year, full 4 digits, e.g. 1999 / 2005 * y : Year, short 2 digits, e.g. 99 / 05 * o : Year as "Y", but day which week is in past year count as that year e.g. the date 1 jan 2006 will return 2005 with "o" * a : Lowercase am or pm * A : Uppercase AM or PM * g : 12 hour clock, without leading zeros, midnight 0, noon 12 * h : 12 hour clock, with leading zeros, midnight 00, noon 12 * G : 24 hour clock, without leading zeros, 0 to 23 * H : 24 hour clock, with leading zeros, 00 to 23 * i : Minutes with leading zeros, 00 to 59 * s : Seconds, with leading zeros, 00 to 59 * e : //none * I : //none * O : Difference to Greenwich time (GMT) in hours, e.g. +0200 or -0300 * Z : Timezone offset in seconds. e.g. -21600 or 10800 * U : Seconds since the Unix Epoch (January 1 1970 00:00:00 GMT) * { : Char after "{" passes direct to out string, e.g. {YY returns Y2006 (if it's 2006) * */ class Date{ long unixTime; long timeZoneOffset = 0; int[] daysInMonth = new int[12] {31,28,31,30,31,30,31,31,30,31,30,31}; long getYear() { return this.unixTime / 31557600; } long getDaysFromEpoch() { return this.unixTime / 86400; } long getHour() { return (this.unixTime / 3600) % 24; } long getMinute() { return (this.unixTime / 60) % 60; } long getSecond() { return this.unixTime % 60; } long getWeekDay() { return (this.getDaysFromEpoch() + 3) % 7; } long getDayInYear() { return (this.unixTime - (this.getYear() * 31536000 + ((this.getYear() + 1) / 4) * 86400)) / 86400; } long getWeek() { if (this.getDayInYear() - this.getWeekDay() < 0){ //if week from last year comes in to this year. return ((((this.unixTime - 604800) - (((this.unixTime - 604800) / 31557600) * 31536000 + ((((this.unixTime - 604800) / 31557600) + 1) / 4) * 86400)) / 86400) - ((((this.unixTime - 604800) / 86400) + 3) % 7)) / 7 + 2; }else{ return (this.getDayInYear() - this.getWeekDay()) / 7 + 1; } } long getMonth() { long tempUnixSecond = this.unixTime - (this.getYear() * 31536000 + ((this.getYear() + 1) / 4) * 86400); int month; if((this.getYear() + 2) % 4 == 0){ this.daysInMonth[1] = 29; } for (month = 0; tempUnixSecond >= this.daysInMonth[month] * 86400; month++) { tempUnixSecond -= this.daysInMonth[month] * 86400; } this.daysInMonth[1] = 28; return month + 1; } long getDay() { long tempUnixSecond = this.unixTime - (this.getYear() * 31536000 + ((this.getYear() + 1) / 4) * 86400); if (this.getMonth() >= 2 && (this.getYear() + 2) % 4 == 0) { tempUnixSecond -= 86400; } for(long n = this.getMonth()-2; n >= 0; n--){ tempUnixSecond -= this.daysInMonth[n] * 86400; } return tempUnixSecond / 86400 + 1; } public string getDate(string referens){ string returnString = ""; for (int n = 0;n < referens.Length; n++){ switch (referens[n]) { case 'j': returnString += this.getDay(); break; case 'd': long tempDay = this.getDay(); if (tempDay < 10){ returnString += "0"; } returnString += tempDay; break; case 'S': switch (this.getDay()) { case 1: returnString += "st"; break; case 2: returnString += "nd"; break; case 3: returnString += "rd"; break; default: returnString += "th"; break; } break; case 'D': switch (this.getWeekDay()){ case 0: returnString += "Mon"; break; case 1: returnString += "Tue"; break; case 2: returnString += "Wed"; break; case 3: returnString += "Thu"; break; case 4: returnString += "Fri"; break; case 5: returnString += "Sat"; break; case 6: returnString += "Sun"; break; } break; case 'l': switch (this.getWeekDay()){ case 0: returnString += "Monday"; break; case 1: returnString += "Tuesday"; break; case 2: returnString += "Wednesday"; break; case 3: returnString += "Thursday"; break; case 4: returnString += "Friday"; break; case 5: returnString += "Saturday"; break; case 6: returnString += "Sunday"; break; } break; case 'w': returnString += this.getWeekDay(); break; case 'z': returnString += this.getDayInYear(); break; case 'W': returnString += this.getWeek(); break; case 'F': switch(this.getMonth()){ case 1: returnString += "January"; break; case 2: returnString += "February"; break; case 3: returnString += "March"; break; case 4: returnString += "April"; break; case 5: returnString += "May"; break; case 6: returnString += "June"; break; case 7: returnString += "July"; break; case 8: returnString += "August"; break; case 9: returnString += "September"; break; case 10: returnString += "October"; break; case 11: returnString += "November"; break; case 12: returnString += "December"; break; } break; case 'M': switch (this.getMonth()) { case 1: returnString += "Jan"; break; case 2: returnString += "Feb"; break; case 3: returnString += "Mar"; break; case 4: returnString += "Apr"; break; case 5: returnString += "May"; break; case 6: returnString += "Jun"; break; case 7: returnString += "Jul"; break; case 8: returnString += "Aug"; break; case 9: returnString += "Sep"; break; case 10: returnString += "Oct"; break; case 11: returnString += "Nov"; break; case 12: returnString += "Dec"; break; } break; case 'm': long tempMonth = this.getMonth(); if (tempMonth < 10){ returnString += "0"; } returnString += tempMonth; break; case 'n': returnString += this.getMonth(); break; case 't': if (this.getMonth() == 1 && (this.getYear() + 2) % 4 == 0) { returnString += "29"; }else{ returnString += this.daysInMonth[this.getMonth()-1]; } break; case 'L': if ((this.getYear() + 2) % 4 == 0) { returnString += "1"; }else{ returnString += "0"; } break; case 'Y': returnString += (this.getYear() + 1970); break; case 'y': returnString += (this.getYear() + 70); break; case 'o': if (this.getDayInYear() - this.getWeekDay() < 0) { returnString += (this.getYear() + 1969); } else { returnString += (this.getYear() + 1970); } break; case 'a': if(this.getHour() < 12){ returnString += "am"; }else{ returnString += "pm"; } break; case 'A': if (this.getHour() < 12){ returnString += "AM"; }else{ returnString += "PM"; } break; case 'g': long tempHour = this.getHour(); if (tempHour >= 13) { tempHour -= 12; } returnString += tempHour; break; case 'h': long tempHour2 = this.getHour(); if (tempHour2 >= 13) { tempHour2 -= 12; } if (tempHour2 < 10) { returnString += "0"; } returnString += tempHour2; break; case 'G': returnString += this.getHour(); break; case 'H': long tempHour3 = this.getHour(); if (tempHour3 < 10) { returnString += "0"; } returnString += tempHour3; break; case 'i': long tempMin = this.getMinute(); if (tempMin < 10) { returnString += "0"; } returnString += tempMin; break; case 's': long tempSec = this.getSecond(); if (tempSec < 10) { returnString += "0"; } returnString += tempSec; break; case 'e': //none break; case 'I': //none break; case 'O': if (timeZoneOffset < 0) { returnString += "-"; }else{ returnString += "+"; } long tempTimeZoneHour = timeZoneOffset / 3600; long tempTimeZoneMin = (timeZoneOffset - tempTimeZoneHour*3600)/60; if (tempTimeZoneHour < 10) { returnString += "0"; } returnString += tempTimeZoneHour; if (tempTimeZoneMin < 10) { returnString += "0"; } returnString += tempTimeZoneMin; break; case 'Z': returnString += timeZoneOffset; break; case 'U': returnString += this.unixTime; break; case '{': if (n + 1 < referens.Length) { returnString += referens[n+1]; n++; } break; default: returnString += referens[n]; break; } } return returnString; } public void setDate(long second, long minute, long hour, long day, long month, long year){ //changes year to years after 1970. if (year >= 100){ //full year, e.g. 1984 year -= 1970; }else if (year < 70){ // short year >= 2000 e.g. 06 year += 30; }else{ //short year >= 1970, < 2000 e.g. 98 year -= 70; } month--; day--; year += month / 12; month %= 12; day += year * 365; day += (year * 12 + month + 22) / 48; //leap year for (int n = 0; n < month; n++){ day += this.daysInMonth[n]; } this.unixTime = second + minute * 60 + hour * 3600 + day * 86400; } public void addDate(long second, long minute, long hour, long day, long month, long year){ year += this.getYear()+1970; month += this.getMonth(); day += this.getDay(); hour += this.getHour(); minute += this.getMinute(); second += this.getSecond(); this.setDate(second, minute, hour, day, month, year); } public void setUDate(long unixStamp){ unixTime = unixStamp; } public void addUDate(long unixStamp){ unixTime += unixStamp; } public void setTimeZone(float timeZoneHour){ timeZoneOffset = (long)(timeZoneHour * 3600); } public void setTimeZoneSec(long timeZone){ timeZoneOffset = timeZone; } }

Edit: blev väldigt långt, men men...