Php

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:

Related Post
<?     
$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'));     
?>

View Comments

  • 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

Recent Posts

Configure and Retrieve S3 Information Using Laravel PHP-AWS-SDK

This tutorial helps integrate a PHP SDK with Laravel. We'll install aws-php-sdk into laravel application and access all aws services… Read More

4 months ago

What is the Purpose of php_eol in PHP?

in this quick PHP tutorial, We'll discuss php_eol with examples. PHP_EOL is a predefined constant in PHP and represents an… Read More

10 months ago

Laravel Table Relationship Methods With Example

This Laravel tutorial helps to understand table Relationships using Elequonte ORM. We'll explore laravel table Relationships usage and best practices… Read More

10 months ago

Exploring the Power of Laravel Eloquent Join?

We'll explore different join methods of Laravel eloquent with examples. The join helps to fetch the data from multiple database… Read More

10 months ago

Quick and Easy Installation of Laravel Valet

in this Laravel tutorial, We'll explore valet, which is a development environment for macOS minimalists. It's a lightweight Laravel development… Read More

11 months ago

What is Laravel Soft Delete and How Does it Work?

I'll go through how to use soft delete in Laravel 10 in this post. The soft deletes are a method… Read More

11 months ago

Categories