PHP Function Mktime() Tutorial with Example

The mktime() returns the Unix time stamp of the given argument. The result time stamp contains the number of seconds between the unix time stamp (January 1 1970 00:00:00 GMT) and the that date. If you will not provide any arguments then value will be set to the current value according to the local date and time.

On success mktime() function return the Unix Timestamp otherwise if any argument is invalid it will return FALSE.

Syntax is of mktime() php method

mktime(hour,minute,second,month,day,year,is_dst)

Parameter 
hourOptional. Specifies the hour
minuteOptional. Specifies the minute
secondOptional. Specifies the second
monthOptional. Specifies the numerical month
dayOptional. Specifies the day
yearOptional. Specifies the year.
is_dstOptional. Set this parameter to 1 if the time is during daylight savings
time (DST), 0 if it is not, or -1 (the default) if it is unknown.

You can also check other recommended Date PHP tutorials,

Below Code is used to convert Time stamp into MySQL type DATE Format

<?=    
$mysql_datetime = date('Y-m-d H:i:s',$timestamp);     
?>

Some Useful Function of DateTime period

Current year

The code below covers the period from January 1st to 00:00:00 today:

<?=
$startTime = mktime(0, 0, 0, 1, 1, date('Y'));     
$endTime = mktime();

Last year

The below code covers date between from January 1, at 00:00:00 to 31 December at 23:59:59:

<?     
$startTime = mktime(0, 0, 0, 1, 1, date('Y')-1);     
$endTime = mktime(23, 59, 59, 12, 31, date('Y')-1);     
?>

This Month

The below code covers the period between from the first day of the current Month to now:

<?     
$startTime = mktime(0, 0, 0, date('m'), 1, date('Y'));     
$endTime = mktime();     
?>

Last 30 days or Last Month

The code below covers the period from 30 days ago to now:

<?      
$ starttime = mktime () - 30 * 3600 * 24;      
$ endTime = mktime ();      
?>

How to get This week DateTime

I will assumes the first day of the week is Monday. It covers the period from Monday morning at 00:00:00 to now:\

<?     
$startTime = mktime(0, 0, 0, date('n'), date('j'), date('Y')) - ((date('N')-1)*3600*24);     
$endTime = mktime();      
?>

Last week(From Monday to Sunday)

Below code covers the period from the Monday at 00:00:00 to the following Sunday at 23:59:59:

<?     
$startTime = mktime(0, 0, 0, date('n'), date('j')-6, date('Y')) - ((date('N'))*3600*24);     
$endTime = mktime(23, 59, 59, date('n'), date('j'), date('Y')) - ((date('N'))*3600*24);     
?>

How to get Last 24 hours Date Time

The code below covers the past 24 hours so far:

<?php     
$startTime = mktime() - 24*3600;     
$endTime = mktime();     
?>

How to get Yesterday Date

Below covers the period from yesterday at 00:00:00 to 23:59:59 yesterday:

<?php     
$startTime = mktime(0, 0, 0, date('m'), date('d')-1, date('Y'));     
$endTime = mktime(23, 59, 59, date('m'), date('d')-1, date('Y'));     
?>

2 thoughts on “PHP Function Mktime() Tutorial with Example

  1. i need the biggest mktime from the list of mk time..
    have u code regarding above..

    below is mktime i need biggest from them using query or mktime syntax
    1365987661
    1365728461
    1365728461
    1365728461
    1365728461
    1365814861
    1365814861
    1365814861
    1365814861
    1365814861

Leave a Reply

Your email address will not be published.