Getting day of week from timestamp PHP - php

i am trying to get the day of week from a timestamp:
an example of the timestamp could be:
2014-09-14 18:28:11
I have tried with the following code:
$date = date("D", strtotime($activity[$i]['timestamp']));
However the result i get here is:
Thu
which should have been sunday?
Also is it possible to get it as a full discription instead of a short version of the day name?

Answer to part two of the question is that you can just use l (lowercase 'L') and it'll output Sunday instead of Sun.
$date = date("l", strtotime($activity[$i]['timestamp']));
As for the first part, it probably output Thursday, 1 January 1970 because it received an error instead of an actual date as argument to strtotime.

Related

PHP doesn't give a proper output for month substraction

Code:
$time = strtotime('2020-03-31');
echo date('Y-m-d', strtotime('-1 month', $time));
Expected Result: Any date from Feb 2020
Actual Result: 2020-03-02
Is there any better way to add or subtract a month from a given date?
Months are an awkward interval to work with, because they don't have a fixed length. Should the algorithm assume that by "1 month" you mean "30 days", or "31 days", or should it just try subtracting 1 from the "month" field in the date structure?
The last option is what is happening here: given "2020-03-31", PHP's date library is subtracting 1 from the "03" to give "2020-02-31". Since that's an invalid date (February 2020 had 29 days), it then "normalises" it to a real date - 2 days after the 29th February was the 2nd March.
Probably you want to use a more specific period to subtract, like 30 days - although note that if the initial input is "2020-03-01" that will give you "2020-01-31", not "2020-02-01".
Ultimately, this is a problem with our irregular calendar, rather than with PHP. It's really up to you to define what you mean by "a month before", and use a more specific algorithm that captures that requirement.
You can make code like below
<?php
$time = strtotime('2020-03-1 -32 days');
echo date('M-Y', $time); // output Feb-2020
?>
The above code will return date as you expected

PHP strtotime returning wrong result when looking for a "first saturday" of a given month [duplicate]

I'm working on a PHP function which calculates holidays:
function holidays($country = 1, $timespan_start = 0, $timespan_end = 0)
The holidays are returned as timestamps in an array.
Since I have to calculate dates like the first Monday of February, I tried strtotime("first monday february $year") and I've discovered that this does not work for 2010, since 02/01/2010 is a Monday - I get February 8th instead.
This bug is actually mentioned in the change log:
In PHP 5 prior to 5.2.7, requesting a given occurrence of a given weekday in a month where that weekday was the first day of the month would incorrectly add one week to the returned timestamp. This has been corrected in 5.2.7 and later versions.
But I'm using PHP 5.3.8.
Why am I experiencing this error?
Looks like you are just missing an "of":
echo date('Y-m-d', strtotime('first monday of february 2010'));
will give the expected result. See the PHP Manual on Relative dates for the various input formats.
Depending on your version of PHP the 'of' statement may or may not work. As another solution try:
echo date('Y-m-d',strtotime('monday February 2010'));
will return the first monday of February 2010. Works for all days as well.

Output of php function "strtotime()" is confused

I have no idea about output of this function.
" strtotime(date("j"),date("F"),date("t")) "
Output: 1475734243
That output is changing according to the time.
my code block is:
<?php
$day= date("j");
$month= date("F");
$year= date("Y");
//calendar Variables
$currentTimeStamp= strtotime($day-$month-$year);
echo $currentTimeStamp;
?>
date("j") gives Day of the month without leading zeros (1 to 31).
date("F") gives A full textual representation of a month, such as January or March.
date("Y") gives A full numeric representation of a year, 4 digits (eg: 2016).
I am not sure what part is confusing but if I got it right that you want to display the current date with that format then you should use this without having to use strtotime()
echo date("j F Y");
Sample working output:
https://repl.it/Dodz/0
The output is a unix time stamp.
echo $day.'-'.$month.'-'.$year;
echo date('j-F-Y', strtotime($currentTimeStamp));
This will give you the answer you are looking for.
See strtotime documentation.
It returns the number of seconds passed since the U-day (January 1 1970 00:00:00 UTC) to the date (in string representation) that is provided as an argument. It's also called "Unix timestamp" and "Unix epoch (time)"

PHP strtotime +1 month adding an extra month [duplicate]

This question already has answers here:
PHP: Adding months to a date, while not exceeding the last day of the month
(7 answers)
Closed 9 years ago.
I have a simple variable that adds one month to today:
$endOfCycle = date("Y-m", strtotime("+1 month"));
Today is January 2013, so I would expect to get back 2013-02 but I'm getting 2013-03 instead. I can't figure out why it's jumping to March.
It's jumping to March because today is 29th Jan, and adding a month gives 29th Feb, which doesn't exist, so it's moving to the next valid date.
This will happen on the 31st of a lot of months as well, but is obviously more noticable in the case of January to Feburary because Feb is shorter.
If you're not interested in the day of month and just want it to give the next month, you should specify the input date as the first of the current month. This will always give you the correct answer if you add a month.
For the same reason, if you want to always get the last day of the next month, you should start by calculating the first of the month after the one you want, and subtracting a day.
This should be
$endOfCycle=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
You can use this code to get the next month:
$ts = mktime(0, 0, 0, date("n") + 1, 1);
echo date("Y-m-d H:i:s", $ts);
echo date("n", $ts);
Assuming today is 2013-01-31 01:23:45 the above will return:
2013-02-01 00:00:00
2
today is 29th of January, +1 month means 29th of Fabruary, but because February consists of 28 days this year, it overlaps to the next day which is March 1st
instead try
strtotime('next month')
Maybe because its 2013-01-29 so +1 month would be 2013-02-29 which doesn't exist so it would be 2013-03-01
You could try
date('m/d/y h:i a',(strtotime('next month',strtotime(date('m/01/y')))));
from the comments on http://php.net/manual/en/function.strtotime.php
$endOfCycle = date("Y-m", mktime(0, 0, 0, date("m", time())+1 , 15, date("m", time())));
try this:
$endOfCycle = date("Y-m", time()+2592000);
this adds 30 days, not exactly a month tough.

PHP strtotime() is not returning the correct month

Being that the current month/year is January 2012, why does the following code return December 2011 and not November 2011?
echo date("F Y", strtotime("-2 months"));
This is on PHP 5.3.0 if it makes a difference.
To get what you are looking for you can use this rather verbose version instead:
echo date("F Y", strtotime("first day of this month - 2 months"));
The problem with your original version is described in detail here: http://derickrethans.nl/obtaining-the-next-month-in-php.html. Quoted below:
Over and over again PHP users complain that next month in PHP's
date-string parser doesn't go to the next month, but instead skips to
the one after next month; like in the following example:
<?php
$d = new DateTime( '2010-01-31' );
$d->modify( 'next month' );
echo $d->format( 'F' ), "\n";
?>
The output of the little script will be March. March obviously doesn't
follow January as February is in between. However, the current
behavior is correct. The following happens internally:
next month increases the month number (originally 1) by one. This
makes the date 2010-02-31. The second month (February) only has 28
days in 2010, so PHP auto-corrects this by just continuing to count
days from February 1st. You then end up at March 3rd. The formatting
strips off the year and day, resulting in the output March. This can
easily be seen when echoing the date with a full date format, which
will output March 3rd, 2010:
This is for adding months, but the same applies in reverse when subtracting months; there was no November 31st, so the strtotime method "corrects" it into December 1st.

Categories