Get unix timestamp using php

Suppose I know that today is Mondays. How to use mktime() in php to get unix timestamp for last friday and friday before that?

Suppose today is the date of 17-01-2011 and its Monday. Then I need a timestamp for 01-14-2011 00:00:00 and 01-01-2011 00:00:00.

+4
source share
3 answers

check strtotime http://php.net/manual/en/function.strtotime.php .. solves most of these problems - otherwise you must bind the date

+7
source

Something like this should

 <?php // Check if today is a Monday if (date('N') == 1) { // Create timestamp for today at 00:00:00 $today = mktime('0', '0', '0', date('n'), date('j'), date('Y')); $last_friday = $today - 60*60*24*3; $last_last_friday = $today - 60*60*24*10; // Convert to a readable format as a check echo 'Last Friday\ timestamp is ' . $last_friday . ' (' . strftime('%d-%m-%Y %H:%M:%S', $last_friday).') <br />'; echo 'Last last Friday\ timestamp is ' . $last_last_friday . ' (' . strftime('%d-%m-%Y %H:%M:%S', $last_last_friday).')'; } 
+1
source

Much easier than you thought (or even me for that matter)! Essentially, strtotime("last friday", time()) gets the timestamp ... last Friday!

Getting the current timestamp:

 $unixNow = time(); echo date('r', $unixNow) . "<br />"; //Thu, 10 Jan 2013 15:14:19 +0000 

Getting last Friday:

 $unixLastFriday = strtotime("last friday", $unixNow); echo date('r', $unixLastFriday) . "<br />"; //Fri, 04 Jan 2013 00:00:00 +0000 

Getting Friday before:

 $unixFridayBeforeThat = strtotime("last friday", $unixLastFriday); echo date('r', $unixFridayBeforeThat) . "<br />"; //Fri, 28 Dec 2012 00:00:00 +0000 
+1
source

Source: https://habr.com/ru/post/1335805/


All Articles