converting UNIX time stamp date to the Monday of that week - php

how can i convert a time stamp to Monday corresponding with that week?
example:
1473750000 (Tue, 13 Sep 2016 07:00:00 GMT)
// coding magic
1473638400 (Mon, 12 Sep 2016 00:00:00 GMT )

To convert timestamps, you can use a PHP Function gmdate(). I already use this on some of my projects/sites.
$timestamp = strtotime("+1 hour"); //Uk Time
echo gmdate("H:i", $timestamp); //Hour:Minutes
For the other parts of gmdate(), check the PHP Manual.

I also add my solution to the problem, using DateInterval.
function getLastMonday($timestamp){
// Initialize DateTime object
$date = new DateTime();
$date->setTimestamp($timestamp);
// Calculate num of days to substract and create date interval
$dayOfWeek = $date->format("w");
$interval = new DateInterval("P".(($dayOfWeek+6)%7).'D');
$interval->invert = 1;
// Substract the Date interval
$date->add($interval);
return $date;
}

I've found an answer using the documentation provided in the answers:
$date = date('Y-m-d',strtotime('this monday this week',1473750000));

Related

PHP convert the day date with the time

I am working on my php script to set up the date with the time. I need some help with convert the day date to the current day and next day date, example: my current time is 15:27 and my current date is 27-11-2019 so when I have the string for the variable get_time1 is 06:00:00, I want to convert it to 28-11-2019 06:00:00. When I have the variable get_time2 that have the time which it is 23:00:00 as my current time is before 23:00:00 so i want to convert the date with the current date with the time to 27-11-2019 23:00:00.
Code:
<?php
$get_time1 = '06:00:00';
$get_time2 = '23:00:00';
date_default_timezone_set('Europe/London');
$Date = date('Y-m-d');
$time = date('H:i:s');
?>
Can you please show me an example how I can set up the day date with the time 06:00:00 and 23:00:00 as if the time 06:00:00 is after 12am to set up the next day date and if the time 23:00:00 is before 12am then set up the time with the current date?
Thank you.
This just creates a DateTime object from the time (which will default it to todays date) and if this is less than the current date and time, it adds 1 day...
$date = DateTime::createFromFormat("H:i:s", $get_time2);
if ( $date < new DateTime() ) {
$date->modify("+1 day");
}
which gives
2019-11-27 23:00:00
and for $get_time1...
2019-11-28 06:00:00
If you use a DateTime that will allow you to do date arithmetic.
$now = new DateTime();
$tomorrow = $now->modify("+1 day");
You can also use strtotime to get a unix timestamp as explained in this answer.
$tomorrow = strtotime('+1 day');
maybe this will do?
$offset = timezone_offset_get( timezone_open( "Europe/London" ), new \DateTime() );
echo 'in London' . gmdate('d-m-Y H:i:s', date( "U" )+$offset);
echo 'current location: ' . date('d-m-Y H:i:s', date( "U" ));

Get Unix timestamp of specific date after another specific time

I can get the for example 19 March of specific date with this code:
$date = strtotime(" 19 March", $current_time);
For example if I gave the unix timestamp of 1st of January of 2010 as an input, It gave me 19 March of 2010. But also if I gave the unix timestamp of 20 March of 2010,I still get 19 March 2010. What I want is to get the next 19 March which in this case, It would be 19 March of 2011.
How can I do that?
Using PHP DateTime this can be achieved as follows:
// New DateTime object
$date = new DateTime('2010-03-19');
// Add a year
$date->add(new DateInterval('P1Y'));
// Output timestamp
echo $date->getTimestamp();
You can do something like as
$get = "19 March";
$given_date = "01 January 2010";
$date_month = date('d F',strtotime($given_date));
$year = date('Y',strtotime($given_date));
if(strtotime($given_date) - strtotime($date_month) < 0){
echo date('l,d F Y',strtotime("$get $year"));
}else{
echo date('l,d F Y',strtotime("$get ".($year+1)));
}
You should first get year from specified date. Then after you can create 19 march date with year and use strtotime() to get timestamp.
//add format according to your current_time variable format
$date = DateTime::createFromFormat("Y-m-d", $current_time);
echo $date->format("Y");
$fixed_date = strtotime($date->format("Y")."-03-19");
You can specify how many days or week you want to add or subtract from a day, as well as set the time with these functions
$nextUpdate = new DateTime("+5 day 1:00 pm");
echo $nextUpdate->getTimestamp();
$nextWeek = new DateTime("+1 week 9:00 am");
echo $nextWeek->getTimestamp();

PHP, need to subtract 12 hours and 30 minutes from a DateTime

I have a PHP DateTime variable.
How can I reduce or subtract 12hours and 30 minutes from this date in at PHP runtime?
Subtract 12 Hours and 30 minutes from a DateTime in PHP:
$date = new DateTime();
$tosub = new DateInterval('PT12H30M');
$date->sub($tosub);
The P stands for Period. The T stands for Timespan.
See DateTime, DateTime::sub, and DateInterval in the PHP manual. You'll have to set the DateTime to the appropriate date and time, of course.
Try with:
$date = new DateTime('Sat, 30 Apr 2011 05:00:00 -0400');
echo $date->format('Y-m-d H:i:s') . "\n";
$date->sub(new DateInterval('PT12H30M'));
echo $date->format('Y-m-d H:i:s') . "\n";
//Result
2011-04-30 05:00:00
2011-04-29 16:30:00
Try strtotime() function:
$source_timestamp=strtotime("Sat, 30 Apr 2011 05:00:00 -0400");
$new_timestamp=strtotime("-12 hour 30 minute", $source_timestamp);
print date('r', $new_timestamp);
Maybe it will be useful for some cases
$date = new DateTime();
$date->modify('-12 hours -30 minutes');
echo $date->format('H:i:s');
try using this instead
//set timezone
date_default_timezone_set('GMT');
//set an date and time to work with
$start = '2014-06-01 14:00:00';
//display the converted time
echo date('Y-m-d H:i',strtotime('+1 hour +20 minutes',strtotime($start)));
If you are not so familiar with the spec of DateInterval like PT12H30M you can proceed with more human readable way using DateInterval::createFromDateString as follows :
$date = new DateTime();
$interval = DateInterval::createFromDateString('12 hour 30 minute');
$date->sub($interval);
Or with direct interval in sub function like below :
$date = new DateTime();
$date->sub(DateInterval::createFromDateString('12 hour 30 minute'));
Store it in a DateTime object and then use the DateTime::sub method to subtract the timespan.
I used in one line, for 12 hours only, and just as an hour display
$date = new DateTime(); $date->modify('-12 hours'); echo $date->format('H')-0;
I used the -0 since sometimes it put a 0 in front of the digit unless I done that, strange.
Here is detailed description of date function,
Using simply strtotime
echo date("Y-m-d H:i:s",strtotime("-12 hour -30 minutes"));
Using DateTime class
$date = new DateTime("-12 hour -30 minutes");
echo $date->format("Y-m-d H:i:s");

PHP - Adding 7 days to a date the proper way [duplicate]

I want to add number of days to current date:
I am using following code:
$i=30;
echo $date = strtotime(date("Y-m-d", strtotime($date)) . " +".$i."days");
But instead of getting proper date i am getting this:
2592000
Please suggest.
This should be
echo date('Y-m-d', strtotime("+30 days"));
strtotime
expects to be given a string containing a US English date format and will try to parse that format into a Unix timestamp (the number of seconds since January 1 1970 00:00:00 UTC), relative to the timestamp given in now, or the current time if now is not supplied.
while date
Returns a string formatted according to the given format string using the given integer timestamp or the current time if no timestamp is given.
See the manual pages for
http://www.php.net/manual/en/function.strtotime.php
http://www.php.net/manual/en/function.date.php
and their function signatures.
This one might be good
function addDayswithdate($date,$days){
$date = strtotime("+".$days." days", strtotime($date));
return date("Y-m-d", $date);
}
$date = new DateTime();
$date->modify('+1 week');
print $date->format('Y-m-d H:i:s');
or print date('Y-m-d H:i:s', mktime(date("H"), date("i"), date("s"), date("m"), date("d") + 7, date("Y"));
$today=date('d-m-Y');
$next_date= date('d-m-Y', strtotime($today. ' + 90 days'));
echo $next_date;
You can add like this as well, if you want the date 5 days from a specific date :
You have a variable with a date like this (gotten from an input or DB or just hard coded):
$today = "2015-06-15"; // Or can put $today = date ("Y-m-d");
$fiveDays = date ("Y-m-d", strtotime ($today ."+5 days"));
echo $fiveDays; // Will output 2015-06-20
Keep in mind, the change of clock changes because of daylight saving time might give you some problems when only calculating the days.
Here's a little php function which takes care of that:
function add_days($date, $days) {
$timeStamp = strtotime(date('Y-m-d',$date));
$timeStamp+= 24 * 60 * 60 * $days;
// ...clock change....
if (date("I",$timeStamp) != date("I",$date)) {
if (date("I",$date)=="1") {
// summer to winter, add an hour
$timeStamp+= 60 * 60;
} else {
// summer to winter, deduct an hour
$timeStamp-= 60 * 60;
} // if
} // if
$cur_dat = mktime(0, 0, 0,
date("n", $timeStamp),
date("j", $timeStamp),
date("Y", $timeStamp)
);
return $cur_dat;
}
You could also try:
$date->modify("+30 days");
You can do it by manipulating the timecode or by using strtotime(). Here's an example using strtotime.
$data['created'] = date('Y-m-d H:i:s', strtotime("+1 week"));
You can use strtotime()
$data['created'] = date('Y-m-d H:m:s', strtotime('+1 week'));
I know this is an old question, but for PHP <5.3 you could try this:
$date = '05/07/2013';
$add_days = 7;
$date = date('Y-m-d',strtotime($date) + (24*3600*$add_days)); //my preferred method
//or
$date = date('Y-m-d',strtotime($date.' +'.$add_days.' days');
You could use the DateTime class built in PHP. It has a method called "add", and how it is used is thoroughly demonstrated in the manual: http://www.php.net/manual/en/datetime.add.php
It however requires PHP 5.3.0.
$date = "04/28/2013 07:30:00";
$dates = explode(" ",$date);
$date = strtotime($dates[0]);
$date = strtotime("+6 days", $date);
echo date('m/d/Y', $date)." ".$dates[1];
You may try this.
$i=30;
echo date("Y-m-d",mktime(0,0,0,date('m'),date('d')+$i,date('Y')));
Simple and Best
echo date('Y-m-d H:i:s')."\n";
echo "<br>";
echo date('Y-m-d H:i:s', mktime(date('H'),date('i'),date('s'), date('m'),date('d')+30,date('Y')))."\n";
Try this
//add the two day
$date = "**2-4-2016**"; //stored into date to variable
echo date("d-m-Y",strtotime($date.**' +2 days'**));
//print output
**4-4-2016**
Use this addDate() function to add or subtract days, month or years (you will need the auxiliar function reformatDate() as well)
/**
* $date self explanatory
* $diff the difference to add or subtract: e.g. '2 days' or '-1 month'
* $format the format for $date
**/
function addDate($date = '', $diff = '', $format = "d/m/Y") {
if (empty($date) || empty($diff))
return false;
$formatedDate = reformatDate($date, $format, $to_format = 'Y-m-d H:i:s');
$newdate = strtotime($diff, strtotime($formatedDate));
return date($format, $newdate);
}
//Aux function
function reformatDate($date, $from_format = 'd/m/Y', $to_format = 'Y-m-d') {
$date_aux = date_create_from_format($from_format, $date);
return date_format($date_aux,$to_format);
}
Note: only for php >=5.3
Use the following code.
<?php echo date('Y-m-d', strtotime(' + 5 days')); ?>
Reference has found from here - How to Add Days to Current Date in PHP
Even though this is an old question, this way of doing it would take of many situations and seems to be robust. You need to have PHP 5.3.0 or above.
$EndDateTime = DateTime::createFromFormat('d/m/Y', "16/07/2017");
$EndDateTime->modify('+6 days');
echo $EndDateTime->format('d/m/Y');
You can have any type of format for the date string and this would work.
//Set time zone
date_default_timezone_set("asia/kolkata");
$pastdate='2016-07-20';
$addYear=1;
$addMonth=3;
$addWeek=2;
$addDays=5;
$newdate=date('Y-m-d', strtotime($pastdate.' +'.$addYear.' years +'.$addMonth. ' months +'.$addWeek.' weeks +'.$addDays.' days'));
echo $newdate;
Do not use php's date() function, it's not as accurate as the below solution and furthermore it is unreliable in the future.
Use the DateTime class
<?php
$date = new DateTime('2016-06-06'); // Y-m-d
$date->add(new DateInterval('P30D'));
echo $date->format('Y-m-d') . "\n";
?>
The reason you should avoid anything to do with UNIX timestamps (time(), date(), strtotime() etc) is that they will inevitably break in the year 2038 due to integer limitations.
The maximum value of an integer is 2147483647 which converts to Tuesday, 19 January 2038 03:14:07 so come this time; this minute; this second; everything breaks
Source
Another example of why I stick to using DateTime is that it's actually able to calculate months correctly regardless of what the current date is:
$now = strtotime('31 December 2019');
for ($i = 1; $i <= 6; $i++) {
echo date('d M y', strtotime('-' . $i .' month', $now)) . PHP_EOL;
}
You'd get the following sequence of dates:
31 December
31 November
31 October
31 September
31 August
31 July
31 June
PHP conveniently recognises that three of these dates are illegal and converts them into its best guess, leaving you with:
01 Dec 19
31 Oct 19
01 Oct 19
31 Aug 19
31 Jul 19
01 Jul 19

How can I find a date after certain weeks?

I need to find a date after a certain number of weeks. For example, if the start date is Saturday, 31 October 2009, and if I choose 16 weeks then I need to find the date after sixteen Saturdays.
Thanks in advance.
You can use strtotime:
// Oct 31 2009 plus 16 weeks
echo date('Y-m-d', strtotime('2009-10-31 +16 week')); // outputs 2010-02-20
// next week
echo date('Y-m-d', strtotime('+1 week')); // outputs 2009-11-07
strtotime() is what you want: it takes an expression and turns it into a date object. Also see date():
$date = '31 october 2009';
$modification = '+ 16 weeks';
echo date('Y-m-d (l)', strtotime("$date")); //2009-10-31 (Saturday)
echo date('Y-m-d (l)', strtotime("$date $modification")); // 2010-02-20 (Saturday)
If you're using PHP 5.2.0 or later, you can use the DateTime class
<?php
$date = new DateTime('31 October 2009'); // This accepts any format that strtotime() does.
// Now add 16 weeks
$date->modify('+16 weeks');
// Now you can output it however you wish
echo $date->format('Y-m-d');
?>
Why not just use unix time, subtract 7 * 24 * 3600 * 16 from that and then convert that back into the date that you need.
This will help with the conversion back:
http://php.net/manual/en/function.date.php

Categories