PHP Date and POSIX date - php

I have two dates (from a datatime picker) that I send using post in PHP to this same page (I use a hidden <input> in a form).
$(function(){
flatpickr('#calendar',{
mode: "range",
onChange: function(dates) {
if(dates.length>1){
$("#formdate .from").val(+dates[0]/1000);
$("#formdate .to").val((+dates[1])/1000);
$("#formdate").submit();
}
}
});
});
Those dates are POSIX date, for example 1490569200 is 2017-03-27 00:00:00.
You can see that I divide them by 1000; it's because I use them in MySQL so it needs to be in second and not in millisecond.
Here is my datetime picker :
When I valid this second date (here the 26th), it call the function above and refresh the page. If I do a echo of my POSIX date I got the following :
1488326400, 1490486400 that are respectively the 1st and the 26th of March (normal behavior). Now I use the following function in PHP to convert them in a readable format :
echo date('l jS \of F Y',$from);
echo date('l jS \of F Y',$to);
It displays me the following :
Wednesday 1st of March 2017
Sunday 26th of March 2017
So until here, everything works fine !
But now if I pick up a date equal or above the 27th of March, here it's what's happen :
I do an echo of my POSIX dates :
1490313600, 1490655600 that are respectively the 24th and 28th (so no problem).
But when I use PHP date for the readable format :
echo date('l jS \of F Y',$from);
echo date('l jS \of F Y',$to);
Friday 24th of March 2017
Monday 27th of March 2017
Indeed, every dates I pick equal or above the 27th will be displayed as the day before.
If I use echo date('l jS \of F Y',1490655600), same issue, it will give me the 27th and no the 28th.
What's wrong ?

As said in the comment, that was due to the winter/summer time.

Related

PHP How To Get Values From Dates

Would be grateful for any assistance with this.
I have:
$value = gmdate("F d Y", getlastmod());
This is returning (for example):-
June 24 2016
My question is how to extract the dayofweek information out of $value?
My output needs to look like:
Friday, June 24, 2016
Is there a way to extract the dayofweek, Month, dayofmonth and year out of $value? If not, how can I get those values from 'getlastmod' ?
Many thanks
First you will need to use strtotime to convert your current $value to timestamp.
Then by using the date()function you can get the day of the week.
Example:
<?php
echo date("l", strtotime("June 24 2016")); // Output: Friday
Try this instead:
$value = gmdate("l, F d, Y", getlastmod());
Output:
Friday, June 24, 2016
$myDate = date_create(getlastmod());
echo $myDate->format('L, F d, Y');
This will print Friday, June 24, 2016
More on date/time formats here:
http://php.net/manual/en/function.date.php
From the PHP formatting guide for date (gmdate is the same as date except it is using GMT) you can use "l" to get the full text of the day of the week.
http://php.net/manual/en/function.date.php
So in your example I think the following should work to meet your output needs.
$value=gmdate("l, F d, Y",getlastmod());

Display todays day of the week, month, day, year in THIS format in PHP?

I would like to display this format of the current date using PHP. I have googled and found a few variances of what I want to do but the company I work for wants it specifically in this format to match what they've mailed out.
Monday, March 16, 2015
Thanks.
If you looked at the PHP documentation for date you'd be able to figure it out very easily:
echo date('l, F j, Y');
which outputs
Monday, March 16, 2015
As mentioned by #alfallouji, you should have a look at the php documentation.
Here is what you need:
$now = new \DateTime();
echo $now->format('l, F j, Y');
The date format string should be "l, F d, Y".
You can display current date in this format as below:
<?php
echo date("l, F d, Y");
?>

Set time in php to 00:00:00

I need to display the time but it must start from 00:00:00? I've got the following but it uses the current time.
print(date("H:i:s"));
As an alternative to mktime(), try the newer DateTime class, eg
$dt = new DateTime;
$dt->setTime(0, 0);
echo $dt->format('H:i:s');
// Add one hour
$dt->add(new DateInterval('PT1H'));
echo $dt->format('H:i:s');
Update
The flexibility of DateInterval makes this a very good candidate for a timer, eg
// add 2 years, 1 day and 9 seconds
$dt->add(new DateInterval('P2Y1DT9S'));
Use mktime() if you want to start with midnight for the current date:
<?php
echo date('l jS \of F Y h:i:s A',mktime(0,0,0));
?>
OUTPUTS
Friday 14th of October 2011 12:00:00 AM
http://codepad.org/s2NrVfRq
In mktime(), you pass arguments for hours, minutes, seconds, month, day, year, so set hours, minutes, seconds to 0 to get today at midnight. (Note, as Phil points out, mktime()'s arguments are optional and you can leave month, day, year out and it will default to the current date).
The mktime() function returns a unix timestamp representing the number of seconds since the unix epoch (January 1, 1970). You can count up from it in seconds or multiples of seconds.
<?php
// $midnight = mktime(0,0,0,date('m'),date('d'),date('Y'));
// The above is equivalent to below
$midnight = mktime(0,0,0);
echo date('l jS \of F Y h:i:s A',$midnight)."\n";
echo date('l jS \of F Y h:i:s A',$midnight+60)."\n"; // One minute
echo date('l jS \of F Y h:i:s A',$midnight+(60*60))."\n"; // One hour
?>
OUTPUTS
Friday 14th of October 2011 12:00:00 AM
Friday 14th of October 2011 12:01:00 AM
Friday 14th of October 2011 01:00:00 AM
http://codepad.org/FTr98z1n
date() uses the current time when you don't pass in an explicit timestamp. See the optional argument in the date documentation.
If you want to explicitly format midnight, use:
date("H:i:s", mktime(0, 0, 0));
try to use this syntax:
print(date("H:i:s", 0));
or
print(date("H:i:s", 10)); // 10 seconds

php date misinterpretation

I run this:
echo date('l, F jS Y','2011-02-12 14:44:00');
I get this:
Wednesday, December 31st 1969
What's wrong?
The second argument for date needs to be a UNIX timestamp, not a date string. Use:
echo date('l, F jS Y', strtotime('2011-02-12 14:44:00'));
http://php.net/manual/en/function.date.php

How to remove repeated day from mysql datetime using php

Following php scripts
<?php
echo date('D, F d ', strtotime($event->starttime)); echo $this->timestamp(strtotime($event->starttime));
?>
display result as,
Mon, March 21 Mon at 8:00 AM
Fri, March 25 Fri at 9:00 AM
etc
Unfortunately its showing day (Here, Mon and Fri) twice. Here how to remove the second day( ie, before string at).
I would like to show output as
Fri, March 25 at 9:00 AM
Any help please...
You don't need two echoes for this, one echo is enough. You can add the 'at' text yourself, if you prepend it with a backslash.
From the PHP Date function:
<?php
echo date('D, F d \a\t H:i A', strtotime($event->starttime));
?>
echo strftime('%a, %B %d at %I:%M %p', strtotime($event->starttime));
A lowercase d passed to date() should return only the day number... check out http://php.net/manual/en/function.date.php for a list of all the valid parameters.
Try removing the space following d?

Categories