Change to hardcoded epoch time in variable? - php

I have a variable called $end_date which gives me the date in epoch format like this: 1539129600. This translates to Wednesday 10 October 2018 00:00:00.
The variable returns different dates, but the time is always the same 00:00:00. Is it possible to take this variable, set the time to 23:59:59 and save it back to a new variable called $end_date_time containing the same date and the new time 23:59:59 in epoch format.

You can do this with the DateTime class, using setTimestamp to convert the timestamp and then setTime to set the time of day, finally getting the new timestamp with format:
$end_date = 1539129600;
$end_of_day = new DateTime(null, new DateTimeZone('UTC'));
$end_of_day->setTimestamp($end_date);
echo $end_of_day->format('Y-m-d H:i:s') . "\n";
$end_of_day->setTime(23,59,59);
echo $end_of_day->format('Y-m-d H:i:s') . "\n";
echo (int)$end_of_day->format('U');
Output:
2018-10-10 00:00:00
2018-10-10 23:59:59
1539215999
Demo on 3v4l.org

Related

php date return 1970-01-01 08:00:00

<?php
echo date("Y-m-d H:i:s", 0);
?>
//Result: 1970-01-01 08:00:00
My question: The second parameter of function date is 0, It should be 1970-01-01 00:00:00.
Why it is 1970-01-01 08:00:00 ?
I set config in php.ini
[Date]
date.timezone=Asia/Ho_Chi_Minh
//Asia/Ho_Chi_Minh: UTC + 7 (GMT + 7)
Please help me.
From the wiki:
The Unix epoch is the time 00:00:00 UTC on 1 January 1970
As you see, the beginning is started by the UTC timezone. That means that it was 8:00 in Asia/Ho_Chi_Minh when the Unix epoch started.
If you need to get 0:00 you have to set negative second parameter
date_default_timezone_set('Asia/Ho_Chi_Minh'); // Set UTC timezone
echo date("Y-m-d H:i:s", -(8 * 60 * 60));
Demo
Date php and Default timezone set if you want to set your current time:
date_default_timezone_set('Asia/Ho_Chi_Minh');
echo date("Y-m-d H:i:s");
you don't need the second parameter.
date() always shows the local time. That’s for you Asia/Ho_Chi_Minh. The gmdate() function is used to display the UTC/GMT time.
echo gmdate("Y-m-d H:i:s", 0);
//Result: 1970-01-01 00:00:00
You can also use DateTime. If DateTime is created from a timestamp, the internal time zone is always UTC.
$dateTime = new DateTime('#0');
echo $dateTime->format("Y-m-d H:i:s");
//Result: 1970-01-01 00:00:00
You don't need to change your default timezone for either option.

PHP convert the day date with the time

I am working on my php script to set up the date with the time. I need some help with convert the day date to the current day and next day date, example: my current time is 15:27 and my current date is 27-11-2019 so when I have the string for the variable get_time1 is 06:00:00, I want to convert it to 28-11-2019 06:00:00. When I have the variable get_time2 that have the time which it is 23:00:00 as my current time is before 23:00:00 so i want to convert the date with the current date with the time to 27-11-2019 23:00:00.
Code:
<?php
$get_time1 = '06:00:00';
$get_time2 = '23:00:00';
date_default_timezone_set('Europe/London');
$Date = date('Y-m-d');
$time = date('H:i:s');
?>
Can you please show me an example how I can set up the day date with the time 06:00:00 and 23:00:00 as if the time 06:00:00 is after 12am to set up the next day date and if the time 23:00:00 is before 12am then set up the time with the current date?
Thank you.
This just creates a DateTime object from the time (which will default it to todays date) and if this is less than the current date and time, it adds 1 day...
$date = DateTime::createFromFormat("H:i:s", $get_time2);
if ( $date < new DateTime() ) {
$date->modify("+1 day");
}
which gives
2019-11-27 23:00:00
and for $get_time1...
2019-11-28 06:00:00
If you use a DateTime that will allow you to do date arithmetic.
$now = new DateTime();
$tomorrow = $now->modify("+1 day");
You can also use strtotime to get a unix timestamp as explained in this answer.
$tomorrow = strtotime('+1 day');
maybe this will do?
$offset = timezone_offset_get( timezone_open( "Europe/London" ), new \DateTime() );
echo 'in London' . gmdate('d-m-Y H:i:s', date( "U" )+$offset);
echo 'current location: ' . date('d-m-Y H:i:s', date( "U" ));

php convert 12hr to 24hr 12:00:am -> 00:00:00

I am getting a datetime format from a form in this format
// $start equals 2014-04-19 12:00:am
// I need to convert to 24hr 2014-04-19 00:00:00
$startconvert = str_replace(':am', '', $start);
$startconverted = date( 'Y-d-m H:i:s', strtotime($startconvert));
echo($startconverted);
//becomes 2014-19-04 12:00:00 which is 12pm
Anyone know how to do this? This looks right to me but it ends up being 12pm
$date = DateTime::createFromFormat('Y-m-d h:i:a', '2014-04-19 12:00:am');
echo $date->format('Y-m-d H:i:s');
This just takes the date you have and reads it in by its parts using DateTime::createFromFormat(). That DateTime object can then be formatted normally.

date returning different results in different versions of php

I have a date A and B.
I wanted to get the hours/minutes between them. Like:
date('h:i', strtotime(B) - strtotime(A));
But I get strange results:
echo date('h:i', strtotime('2014-01-01') - strtotime('2014-01-01'));
// echoes: 01:00 (!)
date_default_timezone_set('Europe/London');
$date = new DateTime();
$A = $date->format('Y-m-d H:i:s'); echo '<br />';
$date->modify("+64 minutes");
$B = $date->format('Y-m-d H:i:s'); echo '<br />';
echo date('h:i', strtotime($B) - strtotime($A));
// echoes 02:04 (!)
Live example for the previous code:
Why is this and how to get the expected result?
This is correct behavior
Why? Think of it: strtotime('2014-01-01') - strtotime('2014-01-01') is zero - but date() expects timestamp as second parameter. So that means, you're trying to get date from zero-point timestamp. And that point is different in different timezones. Your London TZ has +01 offset, that's why 0-point timestamp is 01 Jan 1970 01:00:00 - and that's why date('h:i', 0) is 01:00
Try to set, for example, Moscow zone:
date_default_timezone_set('Europe/Moscow');
$date = new DateTime();
$A = $date->format('Y-m-d H:i:s');
$date->modify("+64 minutes");
$B = $date->format('Y-m-d H:i:s');
echo date('h:i', strtotime($B) - strtotime($A));//04:04
-you'll see exactly 04:04 - because current offset for Moscow is +03 (so 03 hours + 64 minutes modification)
What date expects as its second parameter is an absolute timestamp, which it then formats in the specified format. You outputting h:i means you're outputting only the hour:minute part of a complete year, month, day, hour, minute, second timestamp. If you want to format the relative difference between two timestamps, date is the wrong function to use. The result is expected, since you're actually dealing with absolute timestamps in timezones.

Generate start and ending datetimes in UTC based on a PST date

I am taking in a parameter that is a date in PST timezone which will be in the format of "YYYY-MM-DD" (e.g. "2011-08-15"). This parameter is optional. I have 2 questions that I've been struggling with.
I need to calculate the start and end datetime in UTC for this date.
So if the inputted date is 2011-08-15, I want to get the start and end datetimes:
2011-08-15 07:00:00
2011-08-15 06:59:59
(These are essentially the beginning and end of day)
Second, is to handle the case when the date is not passed in. I want to default to the current PST date them and start from there. So if the current datetime is 2011-08-01 10:00:00, I want to get the same start and end datetimes similar to the first scenario except it's based on the inputted date.
2011-08-01 07:00:00
2011-08-01 06:59:59
I've been pulling my hair out dealing with date and datetime conversions. I'm sure I'm missing something super straightforward.
Parse the date and assume PST timezone:
$date = new DateTime("2011-08-15", new DateTimeZone("PST"));
Change the timezone to UTC: (this does the all conversions for you)
$date->setTimeZone(new DateTimeZone("UTC"));
Calculate start and end. Start is our $date and end is $date + 1 day
$start = $date;
$end = clone $date;
$end->modify("+1 day"); // now $end is $start + 1 day
Print start/end:
printf("start: %s, end: %s\n", $start->format('Y-m-d H:i:s'), $end->format('Y-m-d Hi:s'));
// this prints start: 2011-08-15 07:00:00, end: 2011-08-16 07:00:00
For the last part of your questions, you can easily compare two dates:
if ($date > new DateTime()) { // if $date is after now
// do something
}
So you could do something like that:
if ($date > new DateTime()) {
$date->setTimeZone(new DateTimeZone("UTC"));
}
If you don't much like the OO syntax you could also use the function aliases:
$date = date_create(...);
date_format($date, ...);
date_modify($date, ...);
// ...
Use setTimezone function
Working example (I'm not in PST timezone, so I have to set it explicitly)
$date_input = "20011-09-15";
//$date_input = null; //That will emulate no-input case
date_default_timezone_set("America/Los_Angeles"); //if you are in PST, you don't need this line
$date_start = new DateTime($date_input);
$date_end = new DateTime($date_input);
$date_end->modify("+1 day");
/*$date_start->setTimezone(new DateTimeZone("America/Los_Angeles"));
$date_end->setTimezone(new DateTimeZone("America/Los_Angeles"));*/
//end of date is equal to start of the next day.
//But, if you need something like 2011-08-11 23:59:59 add $date_end->modify('-1 second')
$date_start->setTime(0,0,0);
$date_end->setTime(0,0,0);
echo "Date Start PST:".$date_start->format("Y-m-d H:i:s")."<br/>";
echo "Date End PST:".$date_end->format("Y-m-d H:i:s")."<br/>";
//UTC is equal to London time. Almost :)
$date_start->setTimezone(new DateTimeZone ('Europe/London'));
$date_end->setTimezone(new DateTimeZone ('Europe/London'));
echo "Date Start UTC:".$date_start->format("Y-m-d H:i:s")."<br/>";
echo "Date End UTC:".$date_end->format("Y-m-d H:i:s")."<br/>";

Categories