The Mysolutions

20Apr/110

PHP strtotime

PHP's extremely convenient strtotime() function is adapted from the get_date GNU library. It can convert myriad textual human representations of dates/times into Unix timestamps.

This can be very useful when converting between any of the common date formats above, especially when encountering non- standards-compliant data.

Some examples:
strtotime('2003-07-30 -1 month');
strtotime('1972-09-24');
strtotime('72-9-24');
strtotime('72-09-24');
strtotime('24 September 1972');
strtotime('24 Sept 72');
strtotime('24 Sep 72');
strtotime('Sep 24, 1972');
strtotime('24-sep-72');
strtotime('24sep72');
strtotime('24-sep-72 20:02');
strtotime('24-sep-72 8:02pm');
strtotime('1 year');
strtotime('1 year ago');
strtotime('3 years');
strtotime('2 days');
strtotime('-1 month');
strtotime('now');
strtotime('+1 week');
strtotime("+1 week 3 days 2 hours 8 seconds");
strtotime('next Thursday');
strtotime('last Monday');

Some units of time that strtotime() understands:

* am: the time is before noon
* pm: the time is noon or later
* year: one year; for example, "next year"
* month: one month; for example, "last month"
* fortnight: two weeks; for example, "a fortnight ago"
* week: one week
* day: a day
* hour: an hour
* minute: a minute
* min: same as minute
* second: a second
* sec: same as second

Some relative and ordinal words that strtotime() understands:

* ago: past time relative to now; such as "24 hours ago"
* tomorrow: 24 hours later than the current date and time
* yesterday: 24 hours earlier than the current date and time
* today: the current date and time
* now: the current date and time
* last: modifier meaning "the preceding"; for example, "last tuesday"
* this: the given time during the current day or the next occurrence of the given time; for example, "this 7am" gives the timestamp for 07:00 on the current day, while "this week" gives the timestamp for one week from the current time
* next: modifier meaning the current time value of the subject plus one; for example, "next hour"
* first: ordinal modifier, esp. for months; for example, "May first" (actually, it's just the same as next)
* third: see first (note that there is no "second" for ordinality, since that would conflict with the second time value)
* fourth, fifth, sixth...

Usage tips:

* For 2 digit years, strtotime() assumes 19xx for 69-99 and 20xx for 00-68
* US month/day/year format is acceptable, though it may be ambiguous for many dates if the months are numerical rather than names or abbreviations.
* Always number time interval units such as days, weeks, etc. using numerals rather than spelled out; strtotime() can understand "2 weeks ago" but cannot understand "two weeks ago".
* There are frustrating subtle differences in the flexibility of strtotime() in different version of PHP. If you think you might be testing the limits of what strtotime() can parse, be sure to test, test, test.

Filed under: strtotime No Comments
20Apr/110

php mktime

mktime()

mktime ( [hour [, minute [, second [, month [, day [, year [, daylight savings: 0 for no dst; 1 for dst; -1 dst unknown]]]]]]] )

Examples:

$tomorrow = mktime(0, 0, 0, date("m") , date("d")+1, date("Y"));
$lastmonth = mktime(0, 0, 0, date("m")-1, date("d"), date("Y"));
$nextyear = mktime(0, 0, 0, date("m"), date("d"), date("Y")+1);

Note that these examples will handle daylight savings appropriately as well as overflows, e.g. November 32 will be interpreted as December 1.

20Apr/110

Date Format

MySQL date format:
------------------
date('Y-m-d H:i:s')

- or, for date only: -

date('Y-m-d')

This is the format to use in MySQL SQL statements to express dates in string form.

The documentation does not specify a way to indicate a time zone; the date should be in the time zone that the MySQL server is set to use in order to be correct. If PHP and MySQL are on the same server then date() should normally already express dates using the same time zone as MySQL.

HTTP header date format:
------------------------
gmdate('D, d M Y H:i:s GMT')

This is the format to use for HTTP headers such as Date:, Expires: and Last-Modified:, but not the Set-Cookie:, header which uses a different format, shown in the next section.

Note that this uses the gmdate() function, which is identical to date() except it converts the date to the GMT time zone; also, PHP may report GMT as 'UTC', however the standard requires that UTC always be represented as 'GMT'.

Context example:

HTTP/1.1 200 OK
Date: Mon, 19 Nov 2007 23:47:33 GMT
Server: Apache/1.3.33 (Darwin) PHP/4.4.7
Last-Modified: Mon, 19 Nov 2007 23:40:02 GMT
Accept-Ranges: bytes

HTTP cookie date format:
------------------------
gmdate('D, d-M-Y H:i:s GMT')

This format is for the expiration date of HTTP cookies, and is equivalent to the toGMTString() method of date objects in javascript. Note that this also uses gmdate() and forces 'GMT' instead of 'UTC' as the reported time zone, as required by the specification.

Context example: Set-Cookie: RMID=732423sdfs73242; expires=Fri, 31-Dec-2010 23:59:59 GMT; path=/; domain=.example.net

RSS date format:
----------------
date('D, d M Y H:i:s T')

- or -

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

This date format is used in RSS feeds, in XML elements such as pubDate or lastBuildDate. The date format must be RFC 822 compliant but a 4 digit year is preferred. Unlike the format used by HTTP headers, GMT is not required, and the time zone may be expressed using the either time zone abbreviation ('T') or the offset from GMT ('O').

Context example:

<rss version="2.0">
<channel>
<title>Apple Hot News</title>
<link>http://www.apple.com/hotnews/</link>
<description>Hot News provided by Apple.</description>
<language>en-us</language>
<copyright>Copyright 2007, Apple Inc.</copyright>
<pubDate>Fri, 16 Nov 2007 14:47:14 PST</pubDate>
<lastBuildDate>Fri, 16 Nov 2007 14:47:14 PST</lastBuildDate>

Atom date format:
-----------------
date('c') (PHP 5)

- or -

gmdate('Y-m-dTH:i:s')

Dates in atom files must be compliant with RFC 3339. GMT/UTC is not required.

For versions of PHP earlier than 5.0 and therefore lacking the handy 'c' specifier for ISO 8601 dates, of which RFC 3339 is a subset, getting the time zone specified correctly was tricky for non-GMT dates. Forcing GMT and denoting it as 'Z' will work in all PHP versions.

Context example:

<feed xmlns="http://purl.org/atom/ns#draft-ietf-atompub-format-08">
<title>Example Feed</title>
<link href="http://example.org/"/>
<updated>2003-12-13T18:30:02Z</updated>
<author> <name>John Doe</name> </author>
<entry>
<title>Atom-Powered Robots Run Amok</title>
<link href="http://example.org/2003/12/13/atom03"/>
<id>urn:uuid:1225c695-cfb8-4ebb-aaaa-80da344efa6a</id>
<updated>2003-12-13T18:30:02Z</updated>
<summary>Some text.</summary>
</entry>

Unix/C date format:
-------------------
date('D M j G:i:s T Y')

This is the date format as reported by unix utilities such as date and not coincidentally the format used by ANSI C's asctime() function. The equivalent in the format used by the date utility, as well as the C and alias PHP function strftime(): '%a %b %e %H:%M:%S %Z %Y'.

Example: Mon Nov 19 19:03:52 EST 2007

20Apr/111

PHP Date

Important Full Date and Time:

* r: Displays the full date, time and timezone offset. It is equivalent to manually entering date("D, d M Y H:i:s O")

Time:

* a: am or pm depending on the time
* A: AM or PM depending on the time
* g: Hour without leading zeroes. Values are 1 through 12.
* G: Hour in 24-hour format without leading zeroes. Values are 0 through 23.
* h: Hour with leading zeroes. Values 01 through 12.
* H: Hour in 24-hour format with leading zeroes. Values 00 through 23.
* i: Minute with leading zeroes. Values 00 through 59.
* s: Seconds with leading zeroes. Values 00 through 59.

Day:

* d: Day of the month with leading zeroes. Values are 01 through 31.
* j: Day of the month without leading zeroes. Values 1 through 31
* D: Day of the week abbreviations. Sun through Sat
* l: Day of the week. Values Sunday through Saturday
* w: Day of the week without leading zeroes. Values 0 through 6.
* z: Day of the year without leading zeroes. Values 0 through 365.

Month:

* m: Month number with leading zeroes. Values 01 through 12
* n: Month number without leading zeroes. Values 1 through 12
* M: Abbreviation for the month. Values Jan through Dec
* F: Normal month representation. Values January through December.
* t: The number of days in the month. Values 28 through 31.

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.

Other Formatting:

* U: The number of seconds since the Unix Epoch (January 1, 1970)
* O: This represents the Timezone offset, which is the difference from Greenwich Meridian Time (GMT). 100 = 1 hour, -600 = -6 hours

20Apr/110

Insert Item to a position

Let there is an array having five elements.

$array=array("1","2","3","4","5");

I want to add another element 'janaki' after position 3. So my array should be:

Array
(
[0] => 1
[1] => 2
[2] => 3
[3] => janaki
[4] => 4
[5] => 5
)
So the script is:

<?php
$array=array("1","2","3","4","5");
$pos=3;
$val='janaki';

echo"<pre>";
print_r(array_insert($array,$pos,$val));
echo"</pre>";

function array_insert($array,$pos,$val)
{
$array2 = array_splice($array,$pos);
$array[] = $val;
$array = array_merge($array,$array2);

return $array;
}

?>

Filed under: Array No Comments
20Apr/110

Check for Similar Item

<?php
$os = array("Mac", "NT", "Irix", "Linux");
if (in_array("Irix", $os)) {
echo "Got Irix";
}
if (in_array("mac", $os)) {
echo "Got mac";
}
?>

The second condition fails because in_array() is case-sensitive, so the program above will display:

Got Irix

Filed under: Array No Comments
20Apr/110

Max and Min Value of array

The max()function returns the numerically highest of the parameter values. If multiple values can be considered of the same size, the one that is listed first will be returned.

When max() is given multiple arrays, the longest array is returned. If all the arrays have the same length, max() will use lexicographic ordering to find the return value.

When given a string it will be cast as an integer when comparing.

Example #1 Example uses of max():
---------------------------------
<?php
echo max(1, 3, 5, 6, 7); // 7
echo max(array(2, 4, 5)); // 5

// When 'hello' is cast as integer it will be 0. Both the parameters are equally
// long, so the order they are given in determines the result
echo max(0, 'hello'); // 0
echo max('hello', 0); // hello

echo max('42', 3); // '42'

// Here 0 > -1, so 'hello' is the return value.
echo max(-1, 'hello'); // hello

// With multiple arrays of different lengths, max returns the longest
$val = max(array(2, 2, 2), array(1, 1, 1, 1)); // array(1, 1, 1, 1)

// With multiple arrays of the same length, max compares from left to right
// using lexicographic order, so in our example: 2 == 2, but 4 < 5
$val = max(array(2, 4, 8), array(2, 5, 7)); // array(2, 5, 7)

// If both an array and non-array are given, the array
// is always returned as it's seen as the largest
$val = max('string', array(2, 5, 7), 42); // array(2, 5, 7)
?>

Min () PHP Function:
---------------------
The min () function returns the lowest number in a group or array. Any strings are seen as having a value of 0, and in the case that 0 is the lowest number the first case will be returned. You can also compare multiple arrays. This compares each individual array element, and whichever has a lower number first, is returned as the min. If an array is compared to a non array, the array is always seen as being the largest, and therefor never returned as the min.

Examples:
<?php
echo min(2, 4, 6, 8) ;
// Returns 2

echo min (array(2, 14, 7)) ;
//Returns 2

echo min(0, 'about') ;
//Returns 0

echo min ('about', 0) ;
// Returns about

// Comparing two arrays
// so in our example: 5 = 5, but 7 < 9
$x = max(array(5, 7, 12), array(5, 9, 1)) ;
//Returns array(5, 7, 12)

// If both an array and non-array are given, the array is always seen as the largest, and therefor not returned
$x = max('about', array(2, 4, 6), 99) ;
//Returns about, as it is seen as 0
?>

Filed under: Array No Comments
26Jan/111

Clasic Ajax Script

The Simple Ajax Script is:

function getXmlHttpReq(){
var req;
if(window.XMLHttpRequest && !(window.ActiveXObject)) {
try{
req = new XMLHttpRequest();
}
catch(e){
req = false;
}
// branch for IE/Windows ActiveX version
}
else if(window.ActiveXObject) {
try {
req = new ActiveXObject("Msxml2.XMLHTTP");
}
catch(e) {
try {
req = new ActiveXObject("Microsoft.XMLHTTP");
}
catch(e) {
req = false;
}
}
}
return req;
}

Filed under: Ajax 1 Comment