Php get timestamp of previous day midnight - php

Am trying to get the timestamp of previous day midnight
That is if today is thursday , get the timestamp of wednesday midnight.
How do i go about this
i have tried
$lastdaymidnight = strtotime() //am stuck
What do i need to add to strtotime?

try this
$date = new DateTime();
$date->setTimestamp(strtotime('yesterday midnight'));

One liner:
$date = new DateTime('yesterday midnight');

Related

How to say "next month 15th" in PHP's DateTime?

What I want to do:
Getting a certain day from a certain month directly via the DateTime methods (no mktime stunts :)), like
$day = new DateTime('15th of next month');
but it's not possible to set a fixed day by it's number.
Can anybody help ?
EDIT: I've changed the DateTime from this month to next month to make the problem more clear.
You can use DateTime::createFromFormat(). If you only pass the day value, it'll default to the current month and year.
$date = DateTime::createFromFormat('d', '15');
echo $date->format('Y-m-d'); // 2018-10-15
Edit for the new question requirements:
$date = DateTime::createFromFormat('d', 15)->add(new DateInterval('P1M'));
echo $date->format('Y-m-d'); // 2018-11-15

'Round' up a date to the beginning of the next month

I need to take an existing date from a variable and display a date that is always the 1st day (01) of whatever the next month is (and accounting for the year as well).
So if I have this:
$date = '2017-03-17'; // YYYY-MM-DD
I need to take that date and make it output this:
2017-04-01 // The first day of the next month
Just another example...
$date = '2017-12-23'; // YYYY-MM-DD
...should be converted to...
2018-01-01 // The first day of the next month
You can use DateTime like:
$dateTime = new DateTime('2017-03-17');
$dateTime->modify('first day of next month');
echo $dateTime->format('Y-m-d');
Simply increment the date by 1 month and set the date to 1st of the month. Do -
date('Y-m-01', strtotime('+1 MONTH', strtotime($date)));
Working code
you can get it using
$date = '2017-03-17';
echo date('Y-m-01', strtotime('+1 month',strtotime($date)));
https://eval.in/790237

Get first day date of the week on a given date PHP

I have a date 2015-12-16
i want to get the date of the first day's date for the current week of my date
here 2015-12-16 is in the week 51 of the year then i want to get the first day's date of the week 51 ( 2015-12-14 here)
how could i do it ?
thank you
EDIT: it must work when there are 53 weeks in the year (like in 2015 for example)
You can do the following:
$dateTime = new DateTime("2015-12-16");
$weekNo = $dateTime->format("W");
$newDate = new DateTime();
$newDate->setISODate($dateTime->format("Y"), $weekNo);
Example:
http://sandbox.onlinephpfunctions.com/code/281a1ac298bfee8be421e333e4b7e92c6bb44d65
Since the above is a bit off in some cases here's something more reliable:
$dateTime = new DateTime("2016-01-01");
$dateTime->sub(new DateInterval("P".($dateTime->format("w")-1)."D")); //Since the weekdays are 1-based.
Example:
http://sandbox.onlinephpfunctions.com/code/c5cb0f077fa77974d977ddbffa6bc0b61f9d7851
$date = new \DateTime('2015-12-16');
echo $date->modify('last sunday +1 day')->format('Y-m-d');
This gets start of this week if you count monday to sunday.
Try this:
date('Y-m-d', strtotime('This week', strtotime('2015-12-16')));

php add x weeks to a date and then find the next given day

I can add x week to my date
//$ultima_azione <--- 2015/07/15
//$data['intervallo'] <---- 5
$mydate = date("Y-m-d",strtotime($ultima_azione." +".$data['intervallo']." weeks"));
now how can i give a day starting from that week
example:
//$mydate + "next Monday" -----> final date
and this ve to work like, if today is Monday and i add weeks to jump to an other Monday and then i select the next Monday the week don't ve to change
The simplest way would be to use strtotime. It can do date calculations based on a textual representation of the delta:
$mydate = strtotime('+3 weeks');
It also accepts a second parameter, which is a timestamp to start from when doing the calculation, so after you get the offset in weeks, you can pass the new date to a second calculation:
// Get three weeks from 'now' (no explicit time given)
$mydate = strtotime('+3 weeks');
// Get the Monday after that.
$mydate = strtotime('next Monday', $mydate);
See strtotime documentation for more examples of notations that you can use.
I would highly recommend using PHP's built-in DateTime class for any date and time logic. It's a much better API than the older date and time functions and creates much cleaner and easier to read code.
For example:
// Current date and number of weeks to add
$date = '2015/07/15';
$weeks = 3;
// Create and modify the date.
$dateTime = DateTime::createFromFormat('Y/m/d', $date);
$dateTime->add(DateInterval::createFromDateString($weeks . ' weeks'));
$dateTime->modify('next monday');
// Output the new date.
echo $dateTime->format('Y-m-d');
References:
DateTime.
DateTime::createFromFormat
DateTime::add
DateTime::modify
DateInterval::createFromDateString
DateTime::format
Are you looking for something like this?
$today = time();
$weeks = 2;
// timestamp 2 weeks from now
$futureWeeks = strtotime("+ ".$weeks." weeks");
// the next monday after the timestamp date
$futureMonday = strtotime("next monday",$futureWeeks);
echo date("Y-m-d", $futureMonday);
// or in one line
echo date("Y-m-d", strtotime("next monday", strtotime("+ ".$weeks." weeks")));
PHP is using an unix timestamp for date calculations. Functions as date() and strtotime() using a timestamp as an optional second parameter. This is used a reference for formatting and calculations. If no timestamp is passed to the function the current timestamp is used (time()).
I have the answer here. This will show the next wednesday every 2 weeks and the first date to start from would be the 10th.
I have also added in an estimated delivery which would be 6 weeks after that date.
We will be placing our next order for this on:
<?php
$date = '2020/05/26';
$weeks = 2;
$dateTime = DateTime::createFromFormat('Y/m/d', $date);
$dateTime->add(DateInterval::createFromDateString($weeks . ' weeks'));
$dateTime->modify('wednesday');
echo $dateTime->format('d/m/Y');
?>
Expected delivery for the next order will be:
<?php
$date = '2020/05/26';
$weeks = 2;
$dateTime = DateTime::createFromFormat('Y/m/d', $date);
$dateTime->add(DateInterval::createFromDateString($weeks . ' weeks'));
$dateTime->modify('+42 days next wednesday');
echo $dateTime->format('d/m/Y');
?>
If anyone can confirm this is correct that would be great.

Get Previous and Next Sunday from another time zone

I need a script that will return the next sunday and previous saturday from the date . I know how i can return that value from the current date
$saturday = date('Y-m-d H:i:s',strtotime( "next saturday" ));
$sunday = date('Y-m-d H:i:s',strtotime("previous sunday"));
But i have user and his timezone is different say 'Europe/Zurich'(different timezones will be used)
I need to get his previous sunday's date and next saturday.
I wrote a script like this . But i am not sure about the accuracy of the script since it is timezone related task.Please help me to fix this.
$actual_server_time = new DateTime;
$actual_server_time->setTimeZone(new DateTimeZone('Europe/Zurich'));
$converted_server_date_time = $actual_server_time->format('Y-m-d H:i:s');
$actual_server_time->modify('last Sunday');
$actual_server_time->format('Y-m-d H:i:s');
You can add the timezone in strtotime:
<?php strtotime("previous sunday GMT+1");
DEMO

Categories