Categories

Subscribe via Email

Enter your email address to subscribe and receive notifications of new content by email.

Standard C Time & Date functions

Standard C Time & Date

This section will cover the Time and Date elements of the C Standard Library.

asctime
Syntax
#include <ctime>
char *asctime( const struct tm *ptr );

The function asctime() converts the time in the struct ‘ptr’ to a character string of the following format:

day month date hours:minutes:seconds year

An example:

Mon Jun 26 12:03:53 2000

clock

Syntax
#include <ctime>
clock_t clock( void );

The clock() function returns the processor time since the program started, or -1 if that information is unavailable. To convert the return value to seconds, divide it by CLOCKS_PER_SEC.

Note:
If your compiler and library is POSIX compliant, then CLOCKS_PER_SEC is always defined as 1000000.

ctime

Syntax
#include <ctime>
char *ctime( const time_t *time );

The ctime() function converts the calendar time time to local time of the format:

day month date hours:minutes:seconds year            

using ctime() is equivalent to

asctime( localtime( tp ) );

difftime

Syntax
#include <ctime>
double difftime( time_t time2, time_t time1 );

The function difftime() returns time2time1, in seconds.

gmtime

Syntax
#include <ctime>
struct tm *gmtime( const time_t *time );

The gmtime() function returns the given time in Coordinated Universal Time (usually Greenwich mean time), unless it’s not supported by the system, in which case NULL is returned. Watch out for the static return.

localtime

Syntax
#include <ctime>
struct tm *localtime( const time_t *time );

The function localtime() converts calendar time time into local time. Watch out for the static return.

mktime

Syntax
#include <ctime>
time_t mktime( struct tm *time );

The mktime() function converts the local time in time to calendar time, and returns it. If there is an error, -1 is returned.

setlocale

Syntax
#include <clocale>
char *setlocale( int category, const char * locale );

The setlocale() function is used to set and retrieve the current locale. If locale is NULL, the current locale is returned. Otherwise, locale is used to set the locale for the given category.

category can have the following values:

Value Description
LC_ALL All of the locale
LC_TIME Date and time formatting
LC_NUMERIC Number formatting
LC_COLLATE String collation and regular expression matching
LC_CTYPE Regular expression matching, conversion, case-sensitive comparison, wide character functions, and character classification.
LC_MONETARY For monetary formatting
LC_MESSAGES For natural language messages

strftime

Syntax
#include <ctime>
size_t strftime( char *str, size_t maxsize, const char *fmt, struct tm *time );

The function strftime() formats date and time information from time to a format specified by fmt, then stores the result in str (up to maxsize characters). Certain codes may be used in fmt to specify different types of time:

Code Meaning
 %a abbreviated weekday name (e.g. Fri)
 %A full weekday name (e.g. Friday)
 %b abbreviated month name (e.g. Oct)
 %B full month name (e.g. October)
 %c the standard date and time string
 %d day of the month, as a number (1-31)
 %H hour, 24 hour format (0-23)
 %I hour, 12 hour format (1-12)
 %j day of the year, as a number (1-366)
 %m month as a number (1-12).
 %M minute as a number (0-59)
 %p locale’s equivalent of AM or PM
 %S second as a number (0-59)
 %U week of the year, (0-53), where week 1 has the first Sunday
 %w weekday as a decimal (0-6), where Sunday is 0
 %W week of the year, (0-53), where week 1 has the first Monday
 %x standard date string
 %X standard time string
 %y year in decimal, without the century (0-99)
 %Y year in decimal, with the century
 %Z time zone name
 %% a percent sign

Note:
Some versions of Microsoft Visual C++ may use values that range from 0-11 to describe %m (month as a number).

time

Syntax
#include <ctime>
time_t time( time_t *time );

The function time() returns the current time, or -1 if there is an error. If the argument time is given, then the current time is stored in time.

FacebookTwitterGoogle+PinterestTumblrStumbleUponRedditLinkedInWhatsAppBibSonomyDeliciousDiggDiigoSina WeiboWordPressBlogger PostShare

PHP string function reference

addcslashes()  Returns a string with backslashes in front of the specified characters
addslashes()  Returns a string with backslashes in front of predefined characters
bin2hex()  Converts a string of ASCII characters to hexadecimal values
chop()  Removes whitespace or other characters from the right end of a string
chr()  Returns a character from a specified ASCII value
chunk_split()  Splits a string into a series of smaller parts
convert_cyr_string()  Converts a string from one Cyrillic character-set to another
convert_uudecode()  Decodes a uuencoded string
convert_uuencode()  Encodes a string using the uuencode algorithm
count_chars()  Returns information about characters used in a string
crc32()  Calculates a 32-bit CRC for a string
crypt()  One-way string encryption (hashing)
echo()  Outputs one or more strings
explode()  Breaks a string into an array
fprintf()  Writes a formatted string to a specified output stream
get_html_translation_table()  Returns the translation table used by htmlspecialchars() and htmlentities()
hebrev()  Converts Hebrew text to visual text
hebrevc()  Converts Hebrew text to visual text and new lines (\n) into <br>
hex2bin()  Converts a string of hexadecimal values to ASCII characters
html_entity_decode()  Converts HTML entities to characters
htmlentities()  Converts characters to HTML entities
htmlspecialchars_decode()  Converts some predefined HTML entities to characters
htmlspecialchars()  Converts some predefined characters to HTML entities
implode()  Returns a string from the elements of an array
join()  Alias of implode()
lcfirst()  Converts the first character of a string to lowercase
levenshtein()  Returns the Levenshtein distance between two strings
localeconv()  Returns locale numeric and monetary formatting information
ltrim()  Removes whitespace or other characters from the left side of a string
md5()  Calculates the MD5 hash of a string
md5_file()  Calculates the MD5 hash of a file
metaphone()  Calculates the metaphone key of a string
money_format()  Returns a string formatted as a currency string
nl_langinfo()  Returns specific local information
nl2br()  Inserts HTML line breaks in front of each newline in a string
number_format()  Formats a number with grouped thousands
ord()  Returns the ASCII value of the first character of a string
parse_str()  Parses a query string into variables
print()  Outputs one or more strings
printf()  Outputs a formatted string
quoted_printable_decode()  Converts a quoted-printable string to an 8-bit string
quoted_printable_encode()  Converts an 8-bit string to a quoted printable string
quotemeta()  Quotes meta characters
rtrim()  Removes whitespace or other characters from the right side of a string
setlocale()  Sets locale information
sha1()  Calculates the SHA-1 hash of a string
sha1_file()  Calculates the SHA-1 hash of a file
similar_text()  Calculates the similarity between two strings
soundex()  Calculates the soundex key of a string
sprintf()  Writes a formatted string to a variable
sscanf()  Parses input from a string according to a format
str_getcsv()  Parses a CSV string into an array
str_ireplace()  Replaces some characters in a string (case-insensitive)
str_pad()  Pads a string to a new length
str_repeat()  Repeats a string a specified number of times
str_replace()  Replaces some characters in a string (case-sensitive)
str_rot13()  Performs the ROT13 encoding on a string
str_shuffle()  Randomly shuffles all characters in a string
str_split()  Splits a string into an array
str_word_count()  Count the number of words in a string
strcasecmp()  Compares two strings (case-insensitive)
strchr()  Finds the first occurrence of a string inside another string (alias of strstr())
strcmp()  Compares two strings (case-sensitive)
strcoll()  Compares two strings (locale based string comparison)
strcspn()  Returns the number of characters found in a string before any part of some specified characters are found
strip_tags()  Strips HTML and PHP tags from a string
stripcslashes()  Unquotes a string quoted with addcslashes()
stripslashes()  Unquotes a string quoted with addslashes()
stripos()  Returns the position of the first occurrence of a string inside another string (case-insensitive)
stristr()  Finds the first occurrence of a string inside another string (case-insensitive)
strlen()  Returns the length of a string
strnatcasecmp()  Compares two strings using a “natural order” algorithm (case-insensitive)
strnatcmp()  Compares two strings using a “natural order” algorithm (case-sensitive)
strncasecmp()  String comparison of the first n characters (case-insensitive)
strncmp()  String comparison of the first n characters (case-sensitive)
strpbrk()  Searches a string for any of a set of characters
strpos()  Returns the position of the first occurrence of a string inside another string (case-sensitive)
strrchr()  Finds the last occurrence of a string inside another string
strrev()  Reverses a string
strripos()  Finds the position of the last occurrence of a string inside another string (case-insensitive)
strrpos()  Finds the position of the last occurrence of a string inside another string (case-sensitive)
strspn()  Returns the number of characters found in a string that contains only characters from a specified charlist
strstr()  Finds the first occurrence of a string inside another string (case-sensitive)
strtok()  Splits a string into smaller strings
strtolower()  Converts a string to lowercase letters
strtoupper()  Converts a string to uppercase letters
strtr()  Translates certain characters in a string
substr()  Returns a part of a string
substr_compare()  Compares two strings from a specified start position (binary safe and optionally case-sensitive)
substr_count()  Counts the number of times a substring occurs in a string
substr_replace()  Replaces a part of a string with another string
trim()  Removes whitespace or other characters from both sides of a string
ucfirst()  Converts the first character of a string to uppercase
ucwords()  Converts the first character of each word in a string to uppercase
vfprintf()  Writes a formatted string to a specified output stream
vprintf()  Outputs a formatted string
vsprintf()  Writes a formatted string to a variable
wordwrap()  Wraps a string to a given number of characters
FacebookTwitterGoogle+PinterestTumblrStumbleUponRedditLinkedInWhatsAppBibSonomyDeliciousDiggDiigoSina WeiboWordPressBlogger PostShare

PHP math functions reference

abs()  Returns the absolute (positive) value of a number
acos()  Returns the arc cosine of a number
acosh()  Returns the inverse hyperbolic cosine of a number
asin()  Returns the arc sine of a number
asinh()  Returns the inverse hyperbolic sine of a number
atan()  Returns the arc tangent of a number in radians
atan2()  Returns the arc tangent of two variables x and y
atanh()  Returns the inverse hyperbolic tangent of a number
base_convert()  Converts a number from one number base to another
bindec()  Converts a binary number to a decimal number
ceil()  Rounds a number up to the nearest integer
cos()  Returns the cosine of a number
cosh()  Returns the hyperbolic cosine of a number
decbin()  Converts a decimal number to a binary number
dechex()  Converts a decimal number to a hexadecimal number
decoct()  Converts a decimal number to an octal number
deg2rad()  Converts a degree value to a radian value
exp()  Calculates the exponent of e
expm1()  Returns exp(x) – 1
floor()  Rounds a number down to the nearest integer
fmod()  Returns the remainder of x/y
getrandmax()  Returns the largest possible value returned by rand()
hexdec()  Converts a hexadecimal number to a decimal number
hypot()  Calculates the hypotenuse of a right-angle triangle
is_finite()  Checks whether a value is finite or not
is_infinite()  Checks whether a value is infinite or not
is_nan()  Checks whether a value is ‘not-a-number’
lcg_value()  Returns a pseudo random number in a range between 0 and 1
log()  Returns the natural logarithm of a number
log10()  Returns the base-10 logarithm of a number
log1p()  Returns log(1+number)
max()  Returns the highest value in an array, or the highest value of several specified values
min()  Returns the lowest value in an array, or the lowest value of several specified values
mt_getrandmax()  Returns the largest possible value returned by mt_rand()
mt_rand()  Generates a random integer using Mersenne Twister algorithm
mt_srand()  Seeds the Mersenne Twister random number generator
octdec()  Converts an octal number to a decimal number
pi()  Returns the value of PI
pow()  Returns x raised to the power of y
rad2deg()  Converts a radian value to a degree value
rand()  Generates a random integer
round()  Rounds a floating-point number
sin()  Returns the sine of a number
sinh()  Returns the hyperbolic sine of a number
sqrt()  Returns the square root of a number
srand()  Seeds the random number generator
tan()  Returns the tangent of a number
tanh()  Returns the hyperbolic tangent of a number
INF  INF The infinite PHP 4
M_E  2.7182818284590452354 Returns e PHP 4
M_EULER  0.57721566490153286061 Returns Euler constant PHP 4
M_LNPI  1.14472988584940017414 Returns the natural logarithm of PI: log_e(pi) PHP 5.2
M_LN2  0.69314718055994530942 Returns the natural logarithm of 2: log_e 2 PHP 4
M_LN10  2.30258509299404568402 Returns the natural logarithm of 10: log_e 10 PHP 4
M_LOG2E  1.4426950408889634074 Returns the base-2 logarithm of E: log_2 e PHP 4
M_LOG10E  0.43429448190325182765 Returns the base-10 logarithm of E: log_10 e PHP 4
M_PI  3.14159265358979323846 Returns Pi PHP 4
M_PI_2  1.57079632679489661923 Returns Pi/2 PHP 4
M_PI_4  0.78539816339744830962 Returns Pi/4 PHP 4
M_1_PI  0.31830988618379067154 Returns 1/Pi PHP 4
M_2_PI  0.63661977236758134308 Returns 2/Pi PHP 4
M_SQRTPI  1.77245385090551602729 Returns the square root of PI: sqrt(pi) PHP 5.2
M_2_SQRTPI  1.12837916709551257390 Returns 2/square root of PI: 2/sqrt(pi) PHP 4
M_SQRT1_2  0.70710678118654752440 Returns the square root of 1/2: 1/sqrt(2) PHP 4
M_SQRT2  1.41421356237309504880 Returns the square root of 2: sqrt(2) PHP 4
M_SQRT3  1.73205080756887729352 Returns the square root of 3: sqrt(3) PHP 5.2
NAN  NAN Not A Number PHP 4
PHP_ROUND_HALF_UP  1 Round halves up PHP 5.3
PHP_ROUND_HALF_DOWN  2 Round halves down PHP 5.3
PHP_ROUND_HALF_EVEN  3 Round halves to even numbers PHP 5.3
PHP_ROUND_HALF_ODD  4 Round halves to odd numbers PHP 5.3
FacebookTwitterGoogle+PinterestTumblrStumbleUponRedditLinkedInWhatsAppBibSonomyDeliciousDiggDiigoSina WeiboWordPressBlogger PostShare

PHP date format parameters

date — Format a local time/date

Description

string date ( string $format [, int $timestamp = time() ] )

format
The format of the outputted date string. See the formatting options below.

d  The day of the month (from 01 to 31)
D  A textual representation of a day (three letters)
j  The day of the month without leading zeros (1 to 31)
l (lowercase ‘L’)  A full textual representation of a day
N  The ISO-8601 numeric representation of a day (1 for Monday, 7 for Sunday)
S  The English ordinal suffix for the day of the month (2 characters st, nd, rd or th. Works well with j)
w  A numeric representation of the day (0 for Sunday, 6 for Saturday)
z  The day of the year (from 0 through 365)
W  The ISO-8601 week number of year (weeks starting on Monday)
F  A full textual representation of a month (January through December)
m  A numeric representation of a month (from 01 to 12)
M  A short textual representation of a month (three letters)
n  A numeric representation of a month, without leading zeros (1 to 12)
t  The number of days in the given month
L  Whether it’s a leap year (1 if it is a leap year, 0 otherwise)
o  The ISO-8601 year number
Y  A four digit representation of a year
y  A two digit representation of a year
a  Lowercase am or pm
A  Uppercase AM or PM
B  Swatch Internet time (000 to 999)
g  12-hour format of an hour (1 to 12)
G  24-hour format of an hour (0 to 23)
h  12-hour format of an hour (01 to 12)
H  24-hour format of an hour (00 to 23)
i  Minutes with leading zeros (00 to 59)
s  Seconds, with leading zeros (00 to 59)
u  Microseconds (added in PHP 5.2.2)
e  The timezone identifier (Examples: UTC, GMT, Atlantic/Azores)
I (capital i)  Whether the date is in daylights savings time (1 if Daylight Savings Time, 0 otherwise)
O  Difference to Greenwich time (GMT) in hours (Example: +0100)
P  Difference to Greenwich time (GMT) in hours:minutes (added in PHP 5.1.3)
T  Timezone abbreviations (Examples: EST, MDT)
Z  Timezone offset in seconds. The offset for timezones west of UTC is negative (-43200 to 50400)
c  The ISO-8601 date (e.g. 2013-05-05T16:34:42+00:00)
r  The RFC 2822 formatted date (e.g. Fri, 12 Apr 2013 12:01:05 +0200)
U  The seconds since the Unix Epoch (January 1 1970 00:00:00 GMT)
FacebookTwitterGoogle+PinterestTumblrStumbleUponRedditLinkedInWhatsAppBibSonomyDeliciousDiggDiigoSina WeiboWordPressBlogger PostShare