I'm having a hard time getting dates to stick in variables. When I when I'm tyring to subtract months from a current date it is giving me 0 back (defaulting back to 1-1-1970).
Any thoughts on what I could be doing wrong?
PHP:
$progress = 5;
$initialDate = date('m-d-Y');
echo "ini date: " . date('m-01-Y',$initialDate) . "<br>";
$date = date('m-01-Y', strtotime("-$progress months", strtotime(date('m-d-Y',$initialDate))));
echo "date: " . $date . "<br>";
output:
ini date: 12-01-2014
date: 08-01-1969
The 2nd argument to date() must be in internal Timestamp format, i.e. "number of seconds since the Unix Epoch (January 1 1970 00:00:00 GMT)".
strtotime() is really intended to convert a user input into internal date format. If you want to do math with dates, it is better to use a more precise method. I would suggest reading up on date_add() here: http://php.net/manual/en/datetime.add.php
However, the simplest fix to your code is this:
$progress = 5;
$initialDate = time(); // current timestamp, including seconds
echo "initialDate: $initialDate<br>";
echo "ini date: " . date('m-01-Y',$initialDate) . "<br>";
$date = date('m-d-Y', strtotime("-$progress months", strtotime(date('Y-01-d',$initialDate))));
echo "date: " . $date . "<br>";
Note that I used Y-01-d format for the date fed to strtotime, to go back to the 1st of the month before doing the date math, for two reasons:
Y-m-d format has no danger of being interpreted incorrectly by strtotime, unlike m/d/y and d/m/y.
If you are going back 5 months from Jul 30 to Feb 1, it is safer to have Jul 1 as the intermediate step, not Feb 28. "Safer" meaning you don't even need to test if strtotime handles Feb 28 properly.
You can do this using the DateTime object as well. I find it a little more reliable myself. You basically just initiate a DateTime object and then use its ::sub function to subtract a DateInterval in the amount of $progress months.
$progress = 5;
$initialDate = new DateTime(date("Y-m-01"));
echo "ini date: ".$initialDate->format("m-01-Y")."<br />";
$date = $initialDate->sub(new DateInterval("P".$progress."M"));
echo "date: ".$date->format("m-01-Y")."<br />";
Output:
ini date: 12-01-2014
date: 07-01-2014
Related
My code assumes that zero represents the beginning of the Unix epoch, 1970-01-01 00:00. I upgraded an installation of PHP and now, all of a sudden, zero represents 1970-01-01 01:00 (as verified with date('Y-m-d H:i', 0)). So apparently there is a time zone matter. I put the same code into a sandbox and got 1969-12-31 16:00. I have several unit tests that are broken as a result of this. Time zones do not and should not come into play here.
How can I ensure that date-time functions such as date() always converts zero to 1970-01-01 00:00 regardless of the time zone setting on the particular installation?
Using gmdate() you'll always get 1970-01-01 00:00 for 0, no matter what timezone your server is in:
<?php
date_default_timezone_set('Europe/Berlin');
echo "Europe/Berlin:\n";
echo "gmdate: ".gmdate('d.m.y H:i', 0) . "\n";
echo "date: ".date('d.m.y H:i', 0) . "\n";
date_default_timezone_set('America/Los_Angeles');
echo "\nAmerica/Los_Angeles:\n";
echo "gmdate: ".gmdate('d.m.y H:i', 0) . "\n";
echo "date: ".date('d.m.y H:i', 0) . "\n";
/* OUTPUT:
Europe/Berlin:
gmdate: 01.01.70 00:00
date: 01.01.70 01:00
America/Los_Angeles:
gmdate: 01.01.70 00:00
date: 31.12.69 16:00
*/
https://3v4l.org/FechC
You need to set the default time zone to GMT if you want to use date() like that. For example:
date_default_timezone_set('GMT');
echo date('Y-m-d H:i [I] [e] [O]',0);
The above will show (no matter what the server has been set to):
1970-01-01 00:00 [0] [GMT] [+0000]
Without the date_default_timezone_set('GMT'), or even set to Europe/London, you will get a different result at different times of the year.
From the PHP manual;
date — Format a local time/date
gmdate — Format a GMT/UTC date/time
The solution is to get the timezone setting, set it aside, change the timezone to UTC, perform the calculations, and reset the timezone to its original setting.
So if my original function looked like this:
public function format($argument = null)
{
// Perform some calculations involving date() and strtotime().
return $result;
}
Now it looks like this:
public function format($argument = null)
{
$timezone = date_default_timezone_get();
date_default_timezone_set('UTC');
$result = ...; // Perform some calculations involving date() and strtotime().
date_default_timezone_set($timezone);
return $result;
}
I have a date time like 30-10-2018 06:00:00 pm, Now I want to convert it into UTC timestamp, so I am trying to convert this to strtotime and multiplying it by 1000. But it leading to 6hrs difference in the resulted date
$min_date = "30-10-2018 06:00:00 pm";
$max_date= "30-10-2018 07:00:00 pm";
echo $minDate= strtotime($min_date) * 1000;
echo "<br>";
echo $maxDate= strtotime($max_date) * 1000;
What you want to use is a built-in PHP function known as "gmdate."
Let's use your first timestamp, $min_date, in our example of using gmdate to convert the datetime string to a UTC timestamp:
$min_date = "30-10-2018 06:00:00 pm";
//Use 'gmdate', which accepts a formatting string and time() values as arguments.
$utc_min_date = gmdate("d-m-Y H:i:s", strtotime($min_date));
echo "<p>Before: ".$min_date."</p>";
//Produces: "Before: 30-10-2018 06:00:00 pm"
echo "<p>UTC: ".$utc_min_date."</p>";
//Produces: "UTC: 30-10-2018 23:00:00"
If you're wanting a purely numerical representation, akin to a time() stamp, you can simply convert the resulting UTC timestamp.
$numerical_utc_min_date = strtotime($utc_min_date);
echo "<p>Numerical UTC timestamp: " . $numerical_utc_min_date . "</p>";
//Produces: "Numerical UTC timestamp: 1540958400"
This question already has answers here:
Add number of days to a date
(20 answers)
Closed 6 years ago.
Hello all i am trying to add 30 days to my date. I am using below coding.
<?php
$next_due_date = date('05/06/2016', strtotime("+30 days"));
echo $next_due_date;
?>
But it is returning to "05/06/2016" only!
Please help me!
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
Please try this.
echo date('m/d/Y',strtotime('+30 days',strtotime('05/06/2016'))) . PHP_EOL;
This will return 06/06/2016. Am assuming your initial date was in m/d/Y format. If not, fret not and use this.
echo date('d/m/Y',strtotime('+30 days',strtotime(str_replace('/', '-', '05/06/2016')))) . PHP_EOL;
This will give you the date in d/m/Y format while also assuming your initial date was in d/m/Y format. Returns 05/07/2016
If the input date is going to be in mysql, you can perform this function on mysql directly, like this.
DATE_ADD(due_date, INTERVAL 1 MONTH);
The first parameter is the format, not the current date.
<?php
$next_due_date = date('d/m/Y', strtotime("+30 days"));
echo $next_due_date;
Demo: https://eval.in/583697
If you want to add the 30 days to a particular starting date use the second parameter of the strtotime function to tell it where to start.
When in doubt about how a function works refer to the manual.
http://php.net/manual/en/function.strtotime.phphttp://php.net/manual/en/function.date.php
You need to provide date format like d/m/Y instead of 05/06/2016
Try
$old_date = '05-06-2016';
$next_due_date = date('d-m-Y', strtotime($old_date. ' +30 days'));
echo $next_due_date;
$date = "1998-08-14";
$newdate = strtotime ( '30 day' , strtotime ( $date ) ) ;
$newdate = date ( 'Y-m-j' , $newdate );
echo $newdate;
Found the above code
I am having some trouble trying to convert string to time.
My Code is:
$time = strtotime("14 November, 2013 2:30 AM");
echo $time ."<br />";
echo date("m/d/Y", $time);
I know that strtotime is not magic, and I checked out the acceptable date/time formats but I am not sure how to convert the string to another string without converting it to time first.
What's the easiest way to accomplish this?
Take a look at DateTime::createFromFormat and then call format on the created DateTime instance.
Something like:
$yourTimeString = '14 November, 2013 2:30 AM';
$date = DateTime::createFromFormat('d F, Y h:i A', $yourTimeString);
echo $date->format('m/d/Y');
One way is to rewrite the string using explode and list.
<?php
// here we assume "day month, year time AMPM"
$date = "14 November, 2013 2:30 AM";
// assign a variable to each part of the string
list($day,$month,$year,$time,$ampm) = explode(" ",$date);
// remove the commas at the end of the month
$month = str_replace(',','',$month);
// Now we rewrite the strtotime string
$time = strtotime($month . " " . $day . ", " . $year . " " . $time . " " . $ampm);
echo $time ."<br />";
echo date("m/d/Y", $time);
Php date() function allow natural language string for parsing date,
for Ex:
echo date("d-M-Y", strtotime("first monday of 2019-07")); // returns first monday of july 2019
echo date("d-M-Y", strtotime("last sat of July 2008"));
You can find here php instructions to parsing date as natural languages.
http://php.net/manual/en/datetime.formats.relative.php
this function is used to get current time of my system, I want to get South Africa time, so plz guide me.
$today = time () ;
For example:
<?php
date_default_timezone_set('Africa/Johannesburg');
echo date('Y-m-d H:i:s', time());
$d = new DateTime("now", new DateTimeZone("Africa/Johannesburg"));
echo $d->format("r");
gives
Mon, 07 Jun 2010 02:02:12 +0200
You can change the format. See http://www.php.net/manual/en/function.date.php
time() gives the number of seconds since January 1 1970 00:00:00 GMT (excluding leap seconds), so it doesn't depend on the timezone.
EDIT: For a countdown, you can do:
$tz = new DateTimeZone("Africa/Johannesburg");
$now = new DateTime("now", $tz);
$start = new DateTime("2010-06-11 16:00:00", $tz);
$diff = $start->diff($now);
echo "Days: " . $diff->format("%d") . "\n";
echo "Hours: " . $diff->format("%h") . "\n";
echo "Minutes: " . $diff->format("%i") . "\n";
echo "Seconds: " . $diff->format("%s") . "\n";
The time() function returns the same value all around the world. Its return value is not dependent on the local time zone.
To convert a time value to your local time, call the localtime() function.