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
Related
I have this time format.
For example: 04d 03h 15m,
when obviously is the time left for something to end or expire. How can I convert that to an actual date and time from now using PHP. As in this example, Say today is 19/1/2017 5:45am, 4 days from now is going to be 23rd Jan 2017, 3:15 am.
Thanks in anticipation.
you can do that by using Date() and time() with strtotime() and Try this code
$currenttime = time();
echo date('d/m/y H:i A', $currenttime);
$timeAfterAdding = strtotime("+4 days +3 hours +15 minutes", $currenttime);
$expireTime = date('d/m/y H:i A', $timeAfterAdding);
echo $expireTime;
output:
current time => 19/01/17 05:12 AM
expire time => 23/01/17 09:27 AM
I can get the for example 19 March of specific date with this code:
$date = strtotime(" 19 March", $current_time);
For example if I gave the unix timestamp of 1st of January of 2010 as an input, It gave me 19 March of 2010. But also if I gave the unix timestamp of 20 March of 2010,I still get 19 March 2010. What I want is to get the next 19 March which in this case, It would be 19 March of 2011.
How can I do that?
Using PHP DateTime this can be achieved as follows:
// New DateTime object
$date = new DateTime('2010-03-19');
// Add a year
$date->add(new DateInterval('P1Y'));
// Output timestamp
echo $date->getTimestamp();
You can do something like as
$get = "19 March";
$given_date = "01 January 2010";
$date_month = date('d F',strtotime($given_date));
$year = date('Y',strtotime($given_date));
if(strtotime($given_date) - strtotime($date_month) < 0){
echo date('l,d F Y',strtotime("$get $year"));
}else{
echo date('l,d F Y',strtotime("$get ".($year+1)));
}
You should first get year from specified date. Then after you can create 19 march date with year and use strtotime() to get timestamp.
//add format according to your current_time variable format
$date = DateTime::createFromFormat("Y-m-d", $current_time);
echo $date->format("Y");
$fixed_date = strtotime($date->format("Y")."-03-19");
You can specify how many days or week you want to add or subtract from a day, as well as set the time with these functions
$nextUpdate = new DateTime("+5 day 1:00 pm");
echo $nextUpdate->getTimestamp();
$nextWeek = new DateTime("+1 week 9:00 am");
echo $nextWeek->getTimestamp();
I just tried to subtract 6 and 5 months respectively from current date 08/29/2015 # 11:19am (UTC) and got the same result
Here is the code sample:
date("M, Y", strtotime("-5 months")) // returns Mar, 2015
date("M, Y", strtotime("-6 months")) // returns Mar, 2015
Is it due to day light saving? I think No.
date("M, Y", strtotime("-6 months"))
Simply returns also Mar, 2015 because there was no 29. February this year. So it takes the next month which is March.
To solve this just do it always from the first day of the month, e.g.
echo date("M, Y", strtotime("-6 months", strtotime(date('Y-m-01'))));
//^^^^^^^^^^^^^^^^^^^^^^^^^ First day of month
This is the only working option i could find for you. How to subtract 4 months from today's date?
echo date("Ymd", mktime(0, 0, 0, date("m")-5, date("d"), date("Y")));;
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");
How to find month no,name from week number using php
If you have the ISO week number, then to get the month (of the start of the week) you can use strtotime like:
// F = full name of month, n = month number without leading zero
echo date('F n', strtotime('2010-W50'));
Bear in mind that the ISO week might not be the same as your meaning of week, so read on.
If you want to count the whole weeks since January 1st of this year (regardless of what day of the week that is) then you could do as Adnan mentioned:
echo date('F n', strtotime('1 Jan + 50 weeks'));
echo date('F',strtotime('1 January 2010 +50 weeks'));
www.php.net/date
www.php.net/strtotime
Have a look at php date() - http://php.net/manual/en/function.date.php
Here are some good examples:
<?php
// set the default timezone to use. Available since PHP 5.1
date_default_timezone_set('UTC');
// Prints something like: Monday
echo date("l");
// Prints something like: Monday 8th of August 2005 03:12:46 PM
echo date('l jS \of F Y h:i:s A');
// Prints: July 1, 2000 is on a Saturday
echo "July 1, 2000 is on a " . date("l", mktime(0, 0, 0, 7, 1, 2000));
/* use the constants in the format parameter */
// prints something like: Mon, 15 Aug 2005 15:12:46 UTC
echo date(DATE_RFC822);
// prints something like: 2000-07-01T00:00:00+00:00
echo date(DATE_ATOM, mktime(0, 0, 0, 7, 1, 2000));
?>
$myDate = "2010-05-12";
$weekNumber = date("W", strtotime($myDate));
Just replace the "W" with the value you need. Full reference:
http://php.net/manual/en/function.date.php
If you have a week number, and want the date from it you can use:
date("d m Y", strtotime("1.1.2010 + 30 weeks"));