timestamp delete hours and seconds - php

i have a timestamp 1390107800 is Sun, 19 Jan 2014 05:03:20 GMT How could get this date? Sun, 19 Jan 2014 00:00:00 GMT
I want any given timestamp add the first hour of the day it would be 00:00
thanks.

You should be able to hard-code the zeros without issues but escape the GMT with back-slashes to make them literally show:
echo date('D, d M Y 00:00:00 \G\M\T', 1390107800);
// another option
echo date('D, d M Y', 1390107800).' 00:00:00 GMT';
For future reference: date

Just hardcode the hours minutes and second..?
date("d/m/Y 00:00:00", $timestamp);

Related

How to Convert Date Format in PHP

how to convert Fri Jul 19 2019 00:00:00 GMT 0800 (Singapore Standard Time) this format to mysql date format using PHP?
I tried to use strtotime but none of them working for me
date("Y-m-d H:i:s", strtotime("Fri Jul 19 2019 00:00:00 GMT 0800 (Singapore Standard Time)"));
The date fails to parse because it's in a very weird format.
If you can't control the format of the incoming date you could grab the different parts using regex and parse them:
$rawDate = "Fri Jul 19 2019 00:00:00 GMT 0800 (Singapore Standard Time)";
preg_match('/(.*?) GMT (\d+)\s\(.*?\)/', $rawDate, $matches);
$date = DateTime::createFromFormat('D M j Y H:i:s', $matches[1])
->setTimezone(new DateTimeZone('+' . $matches[2]));
echo $date->format('Y-m-d H:i:s'); // outputs 2019-07-19 06:00:00
Example: https://3v4l.org/fbuMk

how can I format my date in php to have the query new Date() format?

I have a php script that returns the date of the server:
<?php
echo date('D, d M y H:i:s a');
?>
but when I print this value on the client site I get:
Tue, 29 Sep 15 16:19:28 pm
But instead I need the date in this format:
Tue Sep 29 2015 16:18:00 GMT+0200 (Central Europe Daylight Time)
How should I modify my php script then to have it like this?
Thanks!
echo (new DateTime())->format('r');
$datestring = "26-08-2015 03:35:28"; //date as string
$date = new DateTime($datestring); //String to datetime conversion
$date = $date->format('D d M y H:i:s O e'); //format the date
echo $date;
This is the manual link. Timezone you have to set.
Output will look like Wed 26 Aug 15 03:35:28 +0200 Europe/Paris
Go to the very bottom of this page http://php.net/manual/en/function.date-default-timezone-set.php

Relative Date calculation off

I am trying to calculate dates relative to a certain date, but Im getting some very unusual responses. Can someone explain what I am doing wrong? I am US EST if it matters.
<?php
$firstweek_firsttime = date('D M j', strtotime("June 2016 first Sunday"));//June 19th 2016
$firstweek_lasttime = date('D M j', strtotime("June 2016 second Saturday"));
$ret=array(
"Session #1. The week of ".$firstweek_firsttime." to ".$firstweek_lasttime." - ",
"Session #2. The week of ".date('D M j', strtotime("$firstweek_firsttime next Sunday"))." to ".date('D M j', strtotime("$firstweek_lasttime next Saturday"))." - ",
"Session #3. The week of ".date('D M j', strtotime("$firstweek_firsttime +10 day"))." to ".date('D M j', strtotime("$firstweek_lasttime +10 day"))." - "
);
?>
<ul>
<?php
foreach($ret as $wk)
{
?>
<li><?php echo($wk);?></li>
<?php
}
?>
What I am getting:
The week of Sun Jun 19 to Sat Jun 18 -
The week of Thu Jan 1 to Thu Jan 1 -
The week of Wed Jul 1 to Tue Jun 30 -
Goal:
The week of Sun Jun 19 to Sat Jun 25 -
The week of Sun Jun 26 to Sat Jul 2 -
The week of Sun Jul 3 to Sat Jul 9 -
This works for me.
Where "you" are has no bearing on your dates unless you set your timezone.
The date/time is set based on the server location.
It's a bit cumbersome, if I can find a better method I'll update my answer.
UPDATE
strtotime("$firstweek_firsttime"); is the equivalent of writing strtotime("Sun Jun 5");will output 1433635200 (which, as of today, is actually Sun Jun 7 2015 00:00:00) because no year is indicated, the server defaults to the current year
strtotime("next sunday"); will output 1441497600 (which, as of today, is equal to Sun Sep 6 2015 00:00:00
but
strtotime("$firstweek_firsttime next sunday"); is invalid markup and will output nothing
so, since the timestamp is empty the date is automatically set to Jan 1, 1970
The same goes for strtotime("$firstweek_lasttime next Saturday")
strtotime("$firstweek_firsttime +10 days") is the same as strtotime("Sun Jun 5 +10 days")
without a Year the server defaults to the current year and writes it as strtotime("Sun Jun 7 2015 +10 days") because June 7 is the first Sunday for June in 2015
The same goes for strtotime("$firstweek_lasttime +10 day")
All that being said... the simple solution to your question is adding the year to your date format for $firstweek_firsttime and $firstweek_lasttime. This will keep your date in the year that you expect like so...
<?php
$firstweek_firsttime = date('D M j Y', strtotime("June 2016 first Sunday")); // Sun Jun 5 2016
$firstweek_lasttime = date('D M j Y', strtotime("June 2016 second Saturday"));
If you don't want to output the year to the browser simply change your first array item to...
"Session #1. The week of ".date('D M j', strtotime("$firstweek_firsttime"))." to ".date('D M j', strtotime("$firstweek_lasttime"))." - ",
Reference
modify date

Formatting date and time to get date, month, year each in seperate var

In my json response of twitter API I get time stamp like this
Thu Mar 13 14:24:13 +0000 2014
I tried to format in this way:
$created_at = $thing->created_at;
$date = DateTime::createFromFormat('D M d H:m:s O Y', $created_at);
echo $created_at;
echo $date->format('H:m:s');
Which gives result like this:
Thu Mar 13 14:24:13 +0000 2014
2015:12:13 //formated result. How come 2015?????
Wed Mar 12 14:18:14 +0000 2014
2015:06:12
Tue Jan 21 12:50:17 +0000 2014
2018:02:21
Thu Dec 12 09:29:16 +0000 2013
2015:05:12
Why giving wrong result?
I want to get month, year in seperate variable.
You can simplify the creation of the DateTime by doing this:
$dt = new DateTime('#' . strtotime('Thu Mar 13 14:24:13 +0000 2014'));
This parses the date string to a Unix timestamp, and then creates a DateTime object.
echo $dt->format('Y-m-d H:i:s'); // yields the correct result.
You are using month format character m instead of minutes i, thats why you get "wrong" output.
$dt = new DateTime('Thu Mar 13 14:24:13 +0000 2014');
echo $dt->format('H:i:s');

PEAR Date date_create_from_format

Which PHP function do I use to parse a date that is in the format "Fri, 28 Jan 2011 17:57:37 GMT"
strtotime doesn't seem to work for me on that date.
Thanks.
$date = date_create_from_format('D, j M Y H:i:s O', 'Fri, 28 Jan 2011 17:57:37 GMT')
Full docs on the format characters here.
Beats me why couldn't look this up yourself - you've got the function name in your post's title.

Categories