PHP Date not calculating properly - php

I have this code, and it prints out as '01/01/1970'
$dob = mktime(0, 0, 0, date("m"), date("d")-1, date("y")-18);
echo "DOB is ".date("d/m/y", $dob);
Why is the year not 18 years less than today?

date("y") == 11 and 11-18 == -7. You need date("Y") == 2011.
Debugging tip: Print out separate parts of the code so you see what's going on. echo $dob shows that the problem is on the first line, and echo date("y")-18 tells that it's the last argument to mktime() that causes it.

This is the easiest solution :
$dob = strtotime('-18 years');
echo date('d/m/y', $dob);
strtotime() is a powerful function.

try
$dob = mktime(0, 0, 0, date("m"), date("d")-1, date("Y")-18);
echo "DOB is ".date("d/m/y", $dob);

According to the manual, when you specify small y as the argument to date function, it will return two-digit representation of the current year. Since the current year is 2011, it will return 11. Subtracting 18 from it will give you a negative result, that's why mktime is resetting to the original timestamp.
Change date("y") to date("Y"), that is, replace small y with capital Y, then you will get the desired result.

The following code is imho much easier to read:
<?php
$dob = new DateTime();
printf("\nToday is %s", date_format($dob, 'D, M d Y'));
$dob->modify('-1 day');
$dob->modify('-18 year');
printf("\nToday minus %d day and %d year is %s",
-1, -18,
date_format($dob, 'D, M d Y'));
?>
Don't you agree? It is not so difficult to calculate date with PHP. Just look also at the PHP Date function for the various formats, such as Weeknumbers.

Related

Changing Year Specifically in date()

I wish to do an operation on a given date string which needs to change the year of that date to, let's say, 2000. Is there a way to accomplish this feat using PHP's date()?
$old_date = '2017-05-05';
// Needs to change 2017 to 2000.
$new_date = ?
I know one method involves using strtotime($old_date. '-17 years') but its not applicable in every case, such as 2018 which will become 2001. Is there an alternate solution that avoids hardcoding the number of years to subtract?
NOTE: I'm assigning a date string to $old_date for convenience. The actual date is fetched from a table.
Subtract years after millennium
strtotime($old_date . ' -' . date('y', strtotime($old_date)) . 'years')
$time = strtotime($old_date);
$year = 2000;
$new_date = date("Y-m-d", mktime(0, 0, 0, date('n', $time), date('j', $time), $year));

Random Pick a Month From Last 3 Months PHP

I want to random pick a month from this month and last 3 months. Now is February'16, so the last 3 months January'16, December'15 and November'15.
Below is the php I used:
$month = mt_rand(date("m",strtotime("-3 Months")),date('m'));
However I got this error:
mt_rand(): max(2) is smaller than min(11)
because 2nd param is smaller than the 1st param. How to fix this?
I would use the unix timestamps then use the date to format the timestamp.
echo date("m", mt_rand(strtotime("-3 Months"), time()));
Format the date as you choose, http://php.net/manual/en/function.date.php.
am i to verbose with this:
$new_date =date('F Y', mktime(0, 0, 0, date("m")-rand(0,3) , date("d"), date("Y")));
echo $new_date;

How can i convert date("j/n/y") to epoch

I have some data that makes use of date("j/n/y") format i.e date for today is 23/1/15
I have tried
echo strtotime($today);
but this does not give me the timestamp i want.How would i convert a date in date("j/n/y") format to epoch?.
Use DateTime::createFromFormat() to read the date format and then use DateTime::getTimestamp() to format it as a timestamp.
$date = DateTime::createFromFormat('j/n/y', '23/1/15');
$epoch = $date->getTimestamp();
I think you're looking for the mktime function in PHP. It goes a little like this:
$timestamp = mktime(0,0,0,0,0,0);
Where, in order, the arguments are: hour, minute, second, month, day, year. So, in your case:
$today = mktime(0, 0, 0, 1, 23, 2015);
// Would return the timestamp for Jan. 23rd, 2015 at 12:00:00 am (I think)
If you're looking for a dynamic right now timestamp, you may use date() in each of the arguments of mktime. For example:
$rightnow = mktime(date("H"), date("i"), date("s"), date("m"), date("d"), date("Y"));
// Would return the timestamp for Jan. 23rd, 2015 at 10:57:25 am.
But, as John Conde says, it requires you break apart the date before you can use it, so it may not be as efficient.
Hope that helps!
Just to have another approach this one would be good for 85 more years.
$date = date('j/n/y', time());
list($day, $month, $year) = explode("/", $date);
$date = "20" . $year . "-" . $month . "-" . $day;
echo date('m/d/Y', strtotime($date));

convert the month number to date

I am working on a task where I need the user to provide exact number of months it will take them to complete a task and then i need to convert that number to an exact date, so lets suppose a user enters 6, this should give me a date 6 months from now.
I tried the following code looking at different examples on line but I have a feeling the following examples treats the $monthNum as the actual month of a year rather than what I need it to do.
$monthNum = 5;
$monthName = date("F", mktime(0, 0, 0, $monthNum, 10));
echo $monthName;
I will really appreciate any assistance here.
Demo here
Pop in your month in the modify() method.
$monthNum = 6;
$date = new DateTime();
$date->modify(" +{$monthNum} month");
echo $date->format("Y-m-d");
Outputs
2015-05-14
You can try:
$time = new \DateTime('+5 months');
You can use strtotime:
$date = date("Y-m-d", strtotime("+5 months"));
in php 5.4+
echo (new DateTime())->modify('+6 months')->format('d M Y');

Converting separate month, day and year values into a timestamp

I have a month value (1-12), day value (1-31), and a year value (2010,2011,2012). I also have a hour value and a minute value.
How can I give this to strtotime() in a way it can convert it to a timestamp?
why convert string to date when you already know year month and date.
use setDate funtion
<?php
$date = new DateTime();
$date->setDate(2001, 2, 3);
echo $date->format('Y-m-d');
?>
Given the variables $year $month $day $hour $minute you can do:
strtotime("$year-$month-$day $hour:$minute");
Be careful to enclose the variables with ", never in this case with '.
UPDATE (thanks to comments of #Clockwork and #Tadeck):
In case there is a variable $timeofday that represents the time of day (i.e. AM or PM),
then you can parse it this with:
strtotime("$year-$month-$day $hour:$minute$timeofday");
that is to say, just attach that variable to the end of the text.
Is strtotime the best tool for this job? What about mktime()?
$time = mktime($hour, $minute, 0, $month, $day, $year);
You can provide it to function strtotime() in many ways, as mentioned in documentation. Some examples include:
$your_time = strtotime('12/31/2011 9:59');
$your_time = strtotime('2011-12-31 9:59');
$your_time = strtotime('December 31, 2011 9:59');
etc. It really is very flexible.
You can find the list of valid formats in the documentation, and that is (from the "Compound Formats" list in the mentioned documentation) for example:
10/Oct/2000:13:55:36 -0700,
2008:08:07 18:11:31,
2008-08-07 18:11:31,
2008-07-01T22:35:17.02,
2008-07-01T22:35:17.03+08:00,
20080701T22:38:07,
20080701T9:38:07,
20080701t223807,
20080701T093807,
2008-7-1T9:3:37,
(this is really copy of the documentation)
Use it like this strtotime("YYYY-mm-DD HH:MM AM/PM"):
echo date("d F Y h:i:s A", strtotime("2011-06-01 11:15 PM")) . "\n";
OUTPUT
01 June 2011 11:15:00 PM
Y-m-d hh:mm will work
echo strtotime('2011-12-14 11:44 am');
cit #Pekka :)
strtotime($month."-".$day."-".$year)

Categories