-2

The time should be displayed like this 0000:00:0:00:00 to the user which stands for YYYY:WW:D:HH:MM. I will need methods to add/reduce/set the time.

How can I use PHP DateTime() to achieve this? It should always be displayed in this format, even if I only have one hour it should be displayed like this 0000:00:0:01:00.

I appreciate any help!

LF00
  • 27,015
  • 29
  • 156
  • 295
termum
  • 1
  • 2
  • Your question is clear but the comments to the answers below seems as the question is not clear after all. Edit your question with some samples. What should be the input, and what should the output be? – Andreas Nov 09 '19 at 08:29
  • What exactely is not clear? – termum Nov 09 '19 at 08:31
  • Read my question, not just the first sentence. – termum Nov 09 '19 at 08:35
  • As clearly stated, I start with `0000:00:0:00:00`, I need function to edit the time and the output should ALWAYS be in this format, e.g `0000:00:0:01:00` for a day. – termum Nov 09 '19 at 08:36

2 Answers2

0

Try this:

echo date('Y:m:j:h:i');

https://www.php.net/manual/en/function.date.php Second argument for date is timestamp

0

Do it with echo date('Y:W:j:H:i'); PHP Manual date

Y   A full numeric representation of a year, 4 digits           Examples: 1999 or 2003
W   ISO-8601 week number of year, weeks starting on Monday      Example: 42 (the 42nd week in the year)
m   Numeric representation of a month, with leading zeros       01 through 12
j   Day of the month without leading zeros                      1 to 31
H   24-hour format of an hour with leading zeros                00 through 23
i   Minutes with leading zeros                                  00 to 59

For your issue refer to How to create datetime from week number Here is the Demo

$str = '2019-45-09 12:07';
$arr = explode(" ",$str);
$date_arr = explode("-",$arr[0]);
$time_arr = explode(":",$arr[1]);
$date = new DateTime();
$date->setISODate($date_arr[0],$date_arr[1],$date_arr[2])->setTime($time_arr[0],$time_arr[1]);
echo $date->format("Y-W-d H:i:s") . PHP_EOL;
$date->add(new DateInterval("P1D"));
echo $date->format("Y-W-d H:i:s");
LF00
  • 27,015
  • 29
  • 156
  • 295