PHP Put a Date Stamp and a Time Stamp together - php

I got two different timestamps in milliseconds. One is for the chosen DATE. One for the chosen time. I need now to send this to an API and I have to put the DATE and the TIME together. How can I achieve this?
$time = $request -> newTime1; //newTime1 is 1452800524000 which is: Thu Jan 14 2016 20:42:04 GMT+0100 (CET)
$datum = $request -> newDate1; //newDate1 is 1453676400000 which is: Mon Jan 25 2016 00:00:00 GMT+0100 (CET)
$dateformatted = strtotime($datum);
$timeformatted = date ("H:i:s",strtotime($time));
$combinedDT = strtotime("$dateformatted $timeformatted");
$event_start = $combinedDT;
$event_end = $event_start + 3600;
//I want 25.1.2016 20:42

<?php
date_default_timezone_set('CET');
$time = 'Thu Jan 14 2016 20:42:04 GMT+0100 (CET)';
$datum = 'Mon Jan 25 2016 00:00:00 GMT+0100 (CET)';
$t = strtotime($time);
$d = strtotime($datum);
$combined = strftime('%d.', $d) . intval(strftime('%m', $d)) . strftime('.%Y', $d)
. strftime(' %H:%M', $t);
echo $combined;
output
25.1.2016 20:42

I think the main problem is that the time input is multiplied by 100 (not in seconds).
After you solve it the most simple way is too use two date functions
$time = 14528005240;
$datum = 1453676400;
echo $date = date('d.m.Y',$datum).' '.date('H:i',$time);

Related

Convert to DATETIME from "Fri, 10 Jun 2016 14:30:38 -0500"

I am trying to pull RSS data from a page and upload it to my database.
example:
<dc:date>Fri, 10 Jun 2016 14:30:38 -0500</dc:date>
the -0500 is the trouble.
what I usually do:
$dc1 = $xml->channel->item[$i]->children($namespaces["dc"]);
$pubDate1 = $dc1->date;
$pubDate = date('Y-m-d H:i:s', strtotime(str_replace('-', '/', $pubDate1)));
Should I be defining $pubDate differently?
No need of replacing '-' with '/'. You can directly pass the $pubDate1
$dc1 = $xml->channel->item[$i]->children($namespaces["dc"]);
$pubDate1 = $dc1->date; // 'Fri, 10 Jun 2016 14:30:38 -0500'
$pubDate = date('Y-m-d H:i:s', strtotime($pubDate1));
OutPut :
2016-06-11 12:30:38
If all of your date have the last part, maybe this code works. but if the last part(-0500) appears in some of them it wont work:
$dc1 = $xml->channel->item[$i]->children($namespaces["dc"]);
$pubDate1 = explode(' ',$dc1->date);
// remove -0500
array_pop($pubDate1);
$pubDate1 = implode(' ',$pubDate1);
$pubDate = date('Y-m-d H:i:s', strtotime($pubDate1));

PHP Date and Time and timezone

Is there any php function that would convert a date string to its equivalent Asia/Manila time.
I tried setting Asia Manila as my default timezone but no avail.
Please see sample below that needs to be convert to Asia/Manila Time
Sun, 12 Jan 2015 08:27:42 +0000,
Mon, 12 Jan 2015 00:14:04 -0500,
Mon, 12 Jan 2015 05:13:34 +0000 (UTC),
Mon, 12 Jan 2015 08:57:47 +0000 (UTC),
Tue, 13 Jan 2015 01:38:04 +0700 (WIT),
Tue, 13 Jan 2015 00:47:31 +0900 (JST),
Mon, 12 Jan 2015 23:27:26 +0000
your assistance is highy appreciated..
Thanks in advance.
This is the easiest way
date_default_timezone_set('Asia/Manila'); // Set your default TZ to Asia/Manila
// strtotime() will convert all timezones to your default
echo date('Y-m-d H:i:s' , strtotime('Mon, 12 Jan 2015 05:13:34 +0000 (UTC)'));
Try this
$date = new DateTime('2000-01-01', new DateTimeZone('Asia/Manila'));
echo $date->format('Y-m-d H:i:sP') . "\n";
Please try the following
$sdt = '2012-05-15 10:50:00';
$stz = new DateTimeZone('UTC');
$dtz = new DateTimeZone('Asia/Manila');
$dt = new DateTime($sdt, $stz);
$dt->setTimeZone($dtz);
$ddt = $dt->format('Y-m-d H:i:s');
Hope this wil help you.
function convert_timezone($from_tz, $to_tz, $time = '2008-08-03 12:35:23') {
date_default_timezone_set($from_tz);
$datetime = new DateTime($time);
$time_newTZ = new DateTimeZone($to_tz);
$datetime->setTimezone($time_newTZ);
$newDate = $datetime->format('Y-m-d H:i:s');
return $newDate;
}
echo convert_timezone('Asia/Kuala_Lumpur', 'America/Los_Angeles');

Parsing dates with inconsistent formats in PHP

I have two queries, both related to dates.
1) I have dates in these formats, which I'm looking to normalise into the same format before saving into a database:
Saturday 26 July
Monday 28 - Wednesday 30 July
July 24th, 2014
Thu 4 Sep
Thu 28 Aug — Fri 19 Sep
24-07-2014
Single days are quite easy to work out using strtotime(), but ranges of dates are a bit more tricky.
This, for example, doesn't work:
$dateString = "Monday 28 - Wednesday 30 July";
if (strpos($dateString, "-")) {
$datePieces = explode("-", $dateString);
$startDate = strtotime($datePieces[0]);
$endDate = strtotime($datePieces[1]);
} else {
$startDate = strtotime($dateString);
$endDate = strtotime($dateString);
}
echo '<pre>';
echo date('d F Y', $startDate);
echo '<br/>';
echo date('d F Y', $endDate);
Because the month is only on one side of the explode(), doing it this way returns:
01 January 1970
30 July 2014
2) I need a way of working out what year the date is (it will always be in the future). Something along the lines of:
if (the month in the date string has elapsed) {
the year of the date is this year + 1
}
As long as each source provides you with a consistent format you can use DateTime() and DateTime::createFromFormat() to process the dates for you.
//Saturday 26 July
$date = DateTime::createFromFormat('l j F', 'Saturday 26 July');
//July 24th, 2014
$date = new DateTime('July 24th, 2014');
//Thu 4 Sep
$date = DateTime::createFromFormat('D j M', 'Thu 4 Sep');
//Thu 28 Aug — Fri 19 Sep
list($start, $end) = explode(' - ', 'Thu 28 Aug — Fri 19 Sep');
$start = DateTime::createFromFormat('D j M', $start);
$end = DateTime::createFromFormat('D j M', $end);
//24-07-2014
$date = new DateTime('24-07-2014');
I'm going to leave handling Monday 28 - Wednesday 30 July to you since you'll need to do a little more work to get the month from the second date and apply it to the first. But this should show you how to go about this.

PHP Date - Convert from Wed Sep 10 2013 00:00:00 GMT-0500 (EST)

I have a date that is passed to PHP as such:
$date = mysql_real_escape_string($_POST["Date"]);
The date displays like this - Wed Sep 10 2013 00:00:00 GMT-0500 (EST)
Output needed - 2013-09-10
$dt = new DateTime('Wed Sep 10 2013 00:00:00 GMT-0500 (EST)');
echo $dt->format('Y-m-d');
See it in action
<?php
// $date = mysql_real_escape_string($_POST["Date"]);
// You should not use mysql_real_escape_string here.
$date = 'Wed Sep 10 2013 00:00:00 GMT-0500 (EST)';
echo date('Y-m-d', strtotime($date));
$date = mysql_real_escape_string($_POST["Date"]);
echo date('Y-m-d', strtotime($date));

Date formatting in PHP

I've a date formatted like "Tue Jan 05 11:08:27 +0000 2010" and I want to convert it's format to "yyyy-mm-dd 00:00" in PHP.
How can I do that?
convert it to a PHP date object with strtotime() then output it with date()
EDIT
Some more detail; try:
$time = strtotime('Tue Jan 05 11:08:27 +0000 2010');
echo date("Y-m-d h:i", $time);
Y = 4 digit year
m = 2 digit month (with leading 0)
d = 2 digit month (with leading 0)
h = 12 hour time (leading 0)
i = minutes (with leading 0)
http://php.net/manual/en/function.date.php
for all the formatting options
$time_string = 'Tue Jan 05 11:08:27 +0000 2010';
$formated_time = date('Y-m-d h:i', strtotime($time_string));
echo $formated_time;
strtotime + date
Agree with Erik, if you want to do it in one line.
Solution
$date = date('Y-m-d H:i:s', strtotime('Tue Jan 05 11:08:27 +0000 2010'));

Categories