Monday, June 13, 2011

PHP calculate the difference in time between a past date and a future date

  function nicetime($date)
{
    if(empty(
$date)) {
        return
"No date provided";
    }
   
   
$periods         = array("second", "minute", "hour", "day", "week", "month", "year", "decade");
   
$lengths         = array("60","60","24","7","4.35","12","10");
   
   
$now             = time();
   
$unix_date         = strtotime($date);
   
      
// check validity of date
   
if(empty($unix_date)) {   
        return
"Bad date";
    }

   
// is it future date or past date
   
if($now > $unix_date) {   
       
$difference     = $now - $unix_date;
       
$tense         = "ago";
       
    } else {
       
$difference     = $unix_date - $now;
       
$tense         = "from now";
    }
   
    for(
$j = 0; $difference >= $lengths[$j] && $j < count($lengths)-1; $j++) {
       
$difference /= $lengths[$j];
    }
   
   
$difference = round($difference);
   
    if(
$difference != 1) {
       
$periods[$j].= "s";
    }
   
    return
"$difference $periods[$j] {$tense}";
}

$date = "2009-03-04 17:45";
$result = nicetime($date); // 2 days ago

?>

Date Function Formatting of PHP

DAYS
d - day of the month 2 digits (01-31)
j - day of the month (1-31)
D - 3 letter day (Mon - Sun)
l - full name of day (Monday - Sunday)
N - 1=Monday, 2=Tuesday, etc (1-7)
S - suffix for date (st, nd, rd)
w - 0=Sunday, 1=Monday (0-6)
z - day of the year (1=365)
WEEK
W - week of the year (1-52)
MONTH
F - Full name of month (January - December)
m - 2 digit month number (01-12)
n - month number (1-12)
M - 3 letter month (Jan - Dec)
t - Days in the month (28-31)
YEAR
L - leap year (0 no, 1 yes)
o - ISO-8601 year number (Ex. 1979, 2006)
Y - four digit year (Ex. 1979, 2006)
y - two digit year (Ex. 79, 06)
TIME
a - am or pm
A - AM or PM
B - Swatch Internet time (000 - 999)
g - 12 hour (1-12)
G - 24 hour c (0-23)
h - 2 digit 12 hour (01-12)
H - 2 digit 24 hour (00-23)
i - 2 digit minutes (00-59)
s 0 2 digit seconds (00-59)
OTHER
e - timezone (Ex: GMT, CST)
I - daylight savings (1=yes, 0=no)
O - offset GMT (Ex: 0200)
Z - offset in seconds (-43200 - 43200)
r - full RFC 2822 formatted date

Sunday, June 5, 2011

Set unicode database for sqlserver

Right click database--> Property-->Option-->Collation: Corsican_100_BIN2

Database field: Chose NVARCHAR

by cno

Sunday, May 8, 2011

SQL to Select a random row from a database table

There are lots of ways to select a random record or row from a database table. Here are some example SQL statements that don't require additional application logic, but each database server requires different SQL syntax.

Select a random row with MySQL:

SELECT column FROM table
ORDER BY RAND()
LIMIT 1

Select a random row with PostgreSQL:

SELECT column FROM table
ORDER BY RANDOM()
LIMIT 1

Select a random row with Microsoft SQL Server:

SELECT TOP 1 column FROM table
ORDER BY NEWID()

Select a random row with IBM DB2

SELECT column, RAND() as IDX 
FROM table 
ORDER BY IDX FETCH FIRST 1 ROWS ONLY
Thanks Tim

Select a random record with Oracle:

SELECT column FROM
( SELECT column FROM table
ORDER BY dbms_random.value )
WHERE rownum = 1

Saturday, April 30, 2011

Guide:How to connect CentOS to internet (Bridge connection)

This simple guide is for those who cant still connect their CentOS to internet using a router. I'm using "Bridge" connection.

1. Disable first VMware network adapter (right click then click disable).



2.. Right click your Local Area Connection, click properties then tick or enable VMware bridge protocol. click OK



3.Open your Vmware Workstation then change Ethernet to "Bridge".



Click "start this virtual machine".
Login as " root ".

4. (CentOS) Go to System,Administration,Network.
Click DNS tab then check if you have Primary/Secondary DNS & DNS search path filled.


If those are blank then go to you Windows click Start,Run type cmd click ok then on DOS windows type "ipconfig /all"



Save. then try to browse internet. If doesnt work restart CentOS.

Note: If you want to set Centos IP static, the same as the pattern of you windows IP address click Devices then click etho (make sure it is active) then
click edit, click or tick "Statically set IP addresses" then fill in.



Ex. if your Windows ip is 192.168.0.101 then type 192.168.0.102 or (other IP which is not yet assigned by your router to other computer in your network.)
then just copy your Windows Subnet mask and Default gateway.
Click ok then save and try to browse internet, if doesnt work restart CentOS.

Tuesday, February 22, 2011

Using SQLCMD to Execute Multiple SQL Server Scripts

In the root C:\ folder, I create a file called create_db.bat which I use to create the database with all objects 

SQLCMD -E -ddatabasename -ic:\SQL\CMS_Avatar.sql
PAUSE

Double clicking the .bat file, I see that each script processed and that the database created successfully.



Monday, February 14, 2011

Google Talk Hidden Emoticons


 http://mail.google.com/mail/e/350 –
http://mail.google.com/mail/e/351 –
http://mail.google.com/mail/e/352 –
And the list goes on …
Here’s a list of utterly new emoticons/smileys:
Classic
Round
Square
Type this

Cowbell

Cowbell

Cowbell
+/’\

Crab

Crab

Crab
V.v.V

Devil

Devil

Devil
}:-)

Equal sad

Equal sad

Equal sad
=(

Equal slant

Equal slant

Equal slant
=/

Equal tongue

Equal tongue

Equal tongue
=P

Nose sad

Nose sad

Nose sad
:-(

Smile

Smile

Smile
:)

Wince

Wince

Wince
>.<

Nose tongue

Nose tongue

Nose tongue
:-P

Pig
Pig

Pig
:(:)

Broken Heart

Broken Heart

Broken Heart

Kiss

Kiss

Kiss
:-x

Kiss

Kiss

Kiss
:*

Mustache

Mustache

Mustache
:{

Beneath are emoticons from my previous post. For reference I am again displaying them here.

Classic
Round
Square
Type this

Heart/Love

Heart/Love

Heart/Love
<3

It’s a monkey!

It’s a monkey!

It’s a monkey!
:(|)

Rock out.

Rock out.

Rock out.
\m/

Shocked

Shocked

Shocked
:-o

Grin

Grin

Grin
:D

Frown

Frown

Frown
:(

Angry

Angry

Angry
x-(

Cool

Cool

Cool
B-)

Cry

Cry

Cry
:’(

Equal Grin

Equal Grin

Equal Grin
=D

Wink

Wink

Wink
;)

Straight Face

Straight Face

Straight Face
:-|

Equal Smile

Equal Smile

Equal Smile
=)

Nose Grin

Nose Grin

Nose Grin
:-D

Big nose wink

Big nose wink

Big nose wink
;^)

Nose wink

Nose wink

Nose wink
;-)

Nose smile

Nose smile

Nose smile
:-)

Slant

Slant

Slant
:-/

Tongue

Tongue

Tongue
:P