strtotime() Is Adding An Extra Day - php

Can anybody tell me why strtotime() seems to be adding 1 day? This seems to only happen in the late afternoon (something like 7 or 8 PM), otherwise it says the correct day.
echo date('m/d/Y h:i:s a', time());
Output:
12/21/2015 08:34:43 pm
echo gmdate('l, F jS, Y', strtotime(date('m/d/Y h:i:s a', time())));
Output:
Tuesday, December 22nd, 2015
I would like the above output, however, I want today's date (the 21st not the 22nd).

Use date instead of gmdate.
You are using gmdate() which gets the date in UTC. The problem only happens late in the afternoon/evening because at those times it really is the next day in UTC time.
You're also doing too much work - you can simplify that line of code to this:
// echo gmdate('l, F jS, Y', strtotime(date('m/d/Y h:i:s a', time())));
echo date('l, F jS, Y');
Otherwise you've created a timestamp from a time string based on the current time stamp. You could just leave the second parameter to date empty and the current time "now" is assumed.
It is also very important to make sure you are calling date_default_timezone_set somewhere or that you have it configured in your php.ini.

This detail in your code...
echo gmdate('l, F jS, Y', strtotime(date('m/d/Y h:i:s a', time())));
(= the "gmdate") will always return Greenwich Mean Time (GMT), which is London/UK.
So change that to date(....
And add date_default_timezone_set('America/New_York'); anyway...

Decided to ultimately use:
$date = new DateTime(date('Y-m-d'), new DateTimeZone('America/New_York'));
$timestamp = $date->format('U');
$date = gmdate('l, F jS, Y', $timestamp);
based on Alexander's comment.

Related

Today's date format - Day (Shorthand) Date Number Month (Shorthand) - PHP

I have a feed which gives feed in the following format: "Fri 14 Oct"
I want to see if today's date matches the date from the feed. My problem is the format of today's date/
$today = date("d m");
This outputs 17 10.
What is the best way to format $today so that it outputs Day (shorthand) space date (number) Month (shorthand) ?
how about:
$today = date("D j M");
As explained in date() reference manual.
Anyway you should be aware of timezone issues unless you are 100% sure that your server is in the same timezone of the feed you are comparing.
I would follow a different approach though, you can parse the feed's date using DateTime::createFromFormat() which also understand timezones, and then compare it with today's date.
$today = date("D d M");
PHP Date Documentation
<?php
// Prints the day
echo date("l") . "<br>";
// Prints the day, date, month, year, time, AM or PM
echo date("l jS \of F Y h:i:s A");
?>
For more details, please visit http://www.w3schools.com/php/func_date_date.asp

How do I echo out a day in the future using date()?

I want to always echo out the date 3 days from now. So right now I have:
$date = date("l F jS");
echo $date;
Which echos "Friday June 5th"
What exactly do I do so that it echos out "Monday June 8th" and tomorrow it echos out "Tuesday June 9th" (always 3 days ahead).
You can use strtotime() with a relative date format. When you pass a Unix timestamp as the second parameter to date() it will format that date.
$date = date("l F jS", strtotime('+3 days'));
echo $date;
Or if you prefer OOP use DateTime(). With DateTime() you can put the relative date format right into its constructor. It also handles things like daylight savings time which may come into play depending on what you're doing.
$date = new DateTime('+3 days');
echo $date->format("l F jS");

Using strtotime to format date() with AM/PM

I have this line of code I wrote to create a formatted timestamp on emails from my contact us page.
It is working fine, but I'm wondering if it is written poorly and could be reduced into more efficient code? It feels wrong calling date() three times in one line. I'm not a developer by trade.
$timestamp = date('m-d-Y')." ".date('h:i A', strtotime(date('H:i:s')));
which results in: 05-28-2014 03:49 PM
Thoughts?
When you need the current timestamp, you can use,
$timestamp=date("m-d-Y h:i A");
When you need to format the timestamp you fetched from database or other means, you have to use strtotime.
$format_timestamp=date("Y-m-d H:i:s", strtotime($timestamp)); // I just convert your format to YYYY-MM-DD HH:MM:SS format.
Edit:
When you need to subtract x hours from the current time, use
$timestamp=date("m-d-Y h:i A", strtotime("-4 hour"));
Some more examples,
$timestamp=date("m-d-Y h:i A", strtotime("+2 hour")); // Adds 2 hours
$timestamp=date("m-d-Y h:i A", strtotime("+1 day")); // Adds 1 Day
You can simplify making:
$timestamp = date('m-d-Y h:i A');
or
$timestamp = gmdate("M d Y H:i:s");
For more information, see:
PHP Manual
date http://www.php.net/manual/en/function.date.php
gmdate http://www.php.net/manual/en/function.gmdate.php

String date current date/time?

I am using $date = date("D M d, Y G:i");.
When I echo $date, it shows the correct date/time. Now I need this as an string.
I have tried string($date); but nothing happens here. And
$today = strtotime($date);
here I get weird numbers..
I need a string so I can put $today in a message.
What is the correct method for this?
The date() function already returns a string.
Doing this :
$date = date("D M d, Y G:i");
You'll have the current date in the $date variable, as a string -- no need for any additional operation.
If you like working with objects you can do this:
$date = new \DateTime('now');
echo $date->format('D M d, Y G:i');
Your $date variable is a string, there's no need for any conversion.
You can have a look at the documentation: http://ch.php.net/manual/en/function.date.php. The return value of the date() function is string.
The strange numbers you see when you call strtotime() is the Unix timestamp which represents the number of seconds elapsed since January 1 1970 00:00:00 UTC.
You're already getting a string. $date can be used like any string now.
strtotime() actually gives you the number of seconds in time like unix
$date = 'Today is '.date("D M d, Y G:i", time());
echo $date;
With regards to:
$today = strtotime($date);
Those numbers are the current timestamp (the number of seconds since January 1st 1970).
You can use this as a second parameter in the date function to change the date to whatever you want.
$newDate = date("D M d, Y G:i", $timeStamp);

PHP date conversion

I have date in format 2011-01-28 06:34:33 i.e. date("Y-m-d H:i:s"). I want to convert it into 28th January 2011.
How can I change it?
Supply the date function with your format, which can be found here. Pass the timestamp of your original date as the second parameter to date. You can obtain the timestamp by using strtotime.
date("dS F Y", strtotime("2011-01-28 06:34:33"));
Use
$dateStr = date("jS F Y", time());
The day value is without leading zero.
Try this echo date('jS F Y h:i:s A');

Categories