Timestamps of Start and End of Month

Timestamps of start and end of month

You can use mktime and date:

$first_minute = mktime(0, 0, 0, date("n"), 1);
$last_minute = mktime(23, 59, 59, date("n"), date("t"));

That is for the current month. If you want to have it for any month, you have change the month and day parameter accordingly.

If you want to generate it for every month, you can just loop:

$times  = array();
for($month = 1; $month <= 12; $month++) {
$first_minute = mktime(0, 0, 0, $month, 1);
$last_minute = mktime(23, 59, 59, $month, date('t', $first_minute));
$times[$month] = array($first_minute, $last_minute);
}

DEMO

Timestamps of start and end of month in java

You can do something like this

long startDate;
long endDate;

private void calculateMonthStartAndEndTime(int month, int year){
//create the first date of month
Calendar mycal = new GregorianCalendar(year,month, 1);
startDate = mycal.getTimeInMillis();

// Get the number of days in that month which actually gives the last date.
int lastDate = mycal.getActualMaximum(Calendar.DAY_OF_MONTH);
mycal = new GregorianCalendar(year, month, lastDate);
endDate = mycal.getTimeInMillis();
}

Get start & end timestamp for month from Month and Year

Used functions:
http://php.net//manual/ru/function.cal-days-in-month.php
http://php.net/manual/ru/function.mktime.php

<?php
$array = array('Month' => 07, 'Year' => 2014);

$daysCount = cal_days_in_month(CAL_GREGORIAN, $array['Month'], $array['Year']); //obtain month days count

$firstDay = mktime(0, 0, 0, $array['Month'], 1, $array['Year']); //obtain timestamp of specified month first day
$lastDay = mktime(0, 0, 0, $array['Month'], $daysCount, $array['Year']); //obtain timestamp of specified month last day

echo 'First: '.$firstDay.' Last: '.$lastDay;
?>

Getting unix timestamps for the start and end of a month in Python

To get the first day of the next month, one option (there are others) could be:

>>> import datetime
>>> my_date = datetime.datetime(2019, 4, 5)
>>> my_date
datetime.datetime(2019, 4, 5, 0, 0)
>>> first_day_this_month = datetime.datetime(my_date.year, my_date.month, 1)
>>> first_day_this_month
datetime.datetime(2019, 4, 1, 0, 0)

>>> some_day_next_month = first_day_this_month + datetime.timedelta(days=32)
>>> some_day_next_month
datetime.datetime(2019, 5, 3, 0, 0)
>>> first_day_next_month = datetime.datetime(some_day_next_month.year, some_day_next_month.month, 1)
>>> first_day_next_month
datetime.datetime(2019, 5, 1, 0, 0)

Now to get the last second of the current month, one could do:

>>> last_second_this_month = first_day_next_month - datetime.timedelta(seconds=1)
>>> last_second_this_month
datetime.datetime(2019, 4, 30, 23, 59, 59)

>>> import time
>>> time.mktime(last_second_this_month.timetuple())
1556683199.0

how to get the first and last days of a given month

You might want to look at the strtotime and date functions.

<?php

$query_date = '2010-02-04';

// First day of the month.
echo date('Y-m-01', strtotime($query_date));

// Last day of the month.
echo date('Y-m-t', strtotime($query_date));

get the start and end timestamp of the current week in python

You can use datetime.replace():

from datetime import date, datetime, timedelta
today = datetime.now() # or .today()
start = (today - timedelta(days=today.weekday())).replace(hour=0, minute=0, second=0, microsecond=0)
end = (start + timedelta(days=4)).replace(hour=23, minute=59, second=0, microsecond=0)
print("Today: " + str(today))
print("Start: " + str(start))
print("End: " + str(end))

output

Today: 2021-07-08 22:56:19.277242
Start: 2021-07-05 00:00:00
End: 2021-07-09 23:59:00

How can I get the last day of the month's timestamp?

Here's a working example:

// this is if your $current_time is not within the current month (although the code below is valid for every single month and every single timestamp)
$month = date("M", $current_time);

$last_day_timestamp = strtotime('last day of ' . $month);

echo date("d-m-Y", $last_day_timestamp);

// if your $current_time is within the current month
$last_day_timestamp_of_this_month = strtotime('last day of this month')

Also you could use this as a reference for other relative date/time formats:
http://php.net/manual/en/datetime.formats.relative.php

Get first and last date of current month with JavaScript or jQuery

Very simple, no library required:

var date = new Date();
var firstDay = new Date(date.getFullYear(), date.getMonth(), 1);
var lastDay = new Date(date.getFullYear(), date.getMonth() + 1, 0);

or you might prefer:

var date = new Date(), y = date.getFullYear(), m = date.getMonth();
var firstDay = new Date(y, m, 1);
var lastDay = new Date(y, m + 1, 0);

EDIT

Some browsers will treat two digit years as being in the 20th century, so that:

new Date(14, 0, 1);

gives 1 January, 1914. To avoid that, create a Date then set its values using setFullYear:

var date = new Date();
date.setFullYear(14, 0, 1); // 1 January, 14

Get the beginning and ending unix timestamp for a given month and year in php

If you have those dates as strings you can simply use strtotime(), if you have only partial information you can use mktime().

However, September only has 30 days ;)

Example:

$month = 9;
$year = 2010;

$first = mktime(0,0,0,$month,1,$year);
echo date('r', $first);

$last = mktime(23,59,00,$month+1,0,$year);
echo date('r', $last);


Related Topics



Leave a reply



Submit