timestamp(int) to date(string) in php - php

This my code to generate current date.
$now= Time();
$dt=date("d", strtotime($now));
echo $dt;
It outputs 01 as d-m-Y outputs 01-01-1970 instead of current date.
$dt is string and $now is integer type. What should I do to get correct date and get it as integer type.

You don't need to convert time() to strtotime(). strtotime() accepts date as string. time() will return the current time in integer only.
Try with
$date = date('d', time());

You can try this if you want to get current date
$dt=date("d-m-Y", strtotime('today')); //dd-mm-yyyy
echo $dt;
OR
$current_date_in_int = Time();
$current_date_in_string = date('d-m-Y', $current_date_in_int); //dd-mm-yyyy
echo $current_date_in_string;

Related

Issue with converting datetime to Integer

I am facing issue with converting datetime string to integer. Please help. Thanks in advance.
date_default_timezone_set("Europe/London");
$date = date("y:m:d h:i:s"); // string.
$date2 = strtotime($date); // Boolean value. not converted to integer.
echo $date2;
replace the : to -
date_default_timezone_set("Europe/London");
$date = date("y-m-d h:i:s"); // string.
$date2 = strtotime($date); // Boolean value. not converted to integer.
echo $date2;
Your code may be simplified as the following one-liner:
<?php
// 'U' will return UNIX timestamp
echo (new DateTime('now', new DateTimeZone('Europe/London')))->format('U');
date("Y-m-d H:i:s") supplies the current time as string.
strtotime with the current time provides a current timestamp.
The time() function does just that. It returns the current Unix timestamp as integer.
$currentUnixTimestamp = time();
//echo time();

get wrong value after convert date to day

I'm trying to convert date (29/04/2017) to day in week but I get wrong value (Thursday, but need to be Saturday). I check the time of the server, all correct.
my code:
$timestamp = strtotime("29/04/2017");
$day = date('l', $timestamp);
echo $day;
what can be the problem?
When you use slashes in your date, strtotime() assumes MM/DD/YYYY. You either need to change the format of your date or use DateTime::createFromFormat() to parse the date
$timestamp = strtotime("04/29/2017");
$day = date('l', $timestamp);
echo $day;
or
$date = DateTime::createFromFormat('d/m/Y, '29/04/2017);
$day = $date->format('l');
echo $day;

How to extract a day of the month from an RFC 3339-formatted dateTime in PHP?

I'm trying to extract just the day of the month from the dateTime formatted as follows:
$today = date('Y-m-d\TH:i:sP', strtotime('today'));
But, when I try extracting just the day of the month, I get '31'.
I'm using: $day = date('d', $today);
Which, I'm guessing, is incorrect.
This is because the second parameter to date() needs to be a Unix Timestamp. You're passing it string. As a result you get a date of Dec 31, 1969.
All of that code is unnecessary anyways as all you need is:
$day = day('d');
If you're going to only have access to the date string you must convert it to Unix Timestamp before passing it to date().
To extract days (or other parts of a datetime), I use the format function:
$datetime = new DateTime('2000-01-10', new DateTimeZone('Pacific/Nauru'));
$day = $datetime->format('d');
echo $day;
Use any format form the PHP Manual.
Hope this will solve your problem
$today = date('Y-m-d\TH:i:sP', strtotime('today'));
$day = date('d', strtotime($today)); // here is the difference,
// instead of $today use strtotime($today)

Add days to a timestamp

Im trying to add a certain amount of days to a timestmp using this in PHP:
$capturedDate = '2008-06-20';
$endDate = strtotime($capturedDate);
$endDate2 = strtotime('+1 day',$endDate);
echo $endDate2;
but its displaying: 1216526400
any ideas?
Try:
echo date("Y-m-d H:i:s",$endDate2);
Or (for just the date):
echo date("Y-m-d",$endDate2);
You can find documentation about how to format your string here: http://php.net/manual/en/function.date.php
You should be using DateTime for working with dates. It's timezone friendly.
$datetime = new DateTime('2008-06-20');
$datetime->modify('+1 day');
echo $datetime->getTimestamp();
strtotime() converts the date into a unix timestamp which is the number of seconds since January 1st 1970. If you want a date output you have to run the finished timestamp through date() first.
$capturedDate = '2008-06-20';
$endDate = strtotime($capturedDate.' +1 day');
echo date("Y-m-d", $endDate);
strtotime creates a Unix timestamp so if you want to be presented with a formatted date, you need to pass the timestamp as an argument to the date function as follows:
$capturedDate = '2008-06-20';
$endDate = strtotime($capturedDate);
$endDate2 = strtotime('+1 day',$endDate);
echo date('Y-m-d', $endDate2);
Additionally, there are a wide variety of parameters you can use in the date function if you want to display additional information.
e.g.: echo date('Y-m-d H:i:s', $endDate2); or echo date('Y-m-d h:i:s a', $endDate2);, etc.
Sooooo close, just take your timestamp and convert it back into date format using date("desired format",$endDate2);
DateTime is a very nice way to deal with dates. You can try like this:
$capturedDate = '2008-06-20';
$date = DateTime::createFromFormat('Y-m-d', $capturedDate)->modify('+1 day');
echo $date->getTimestamp();

Add minutes to current time

I am trying to add minutes to current date but it returns strange results
date_default_timezone_set('Asia/Karachi');
$currentDate = date("m-d-Y H:i:s");
$currentDate_timestamp = strtotime($currentDate);
$endDate_months = strtotime("+10 minutes", $currentDate_timestamp);
$packageEndDate = date("m-d-Y H:i:s", $endDate_months);
echo " <br> " . $packageEndDate . " <br> ";
echo $currentDate;
I am getting Output
01-01-1970 05:50:00
07-19-2013 20:25:23
It should return
07-19-2013 20:35:23
07-19-2013 20:25:23
After this I need to query to database so date format should be same. Database column is of string type.
Your code is redundant. Why format a timestamp as a string, then convert that string back to a timestamp?
Try
$now = time();
$ten_minutes = $now + (10 * 60);
$startDate = date('m-d-Y H:i:s', $now);
$endDate = date('m-d-Y H:i:s', $ten_minutes);
instead.
Probably the minimalist way would be:
date_default_timezone_set('Asia/Baku');
$packageEndDate = date('Y-m-d H:i:s', strtotime('+10 minute'));
echo $packageEndDate;
Output (Current time in my city at the time of writing):
2017-07-20 12:45:17
Try this:
$now = time();
$tenMinFromNow = date("m-d-Y H:i:s", strtotime('+10 minutes', $time));
$tenMinsFromNow = (new \DateTime())->add(new \DateInterval('PT10M'));
Will leave you with a DateTime object representing a time 10 minutes in the future. Which will allow you to do something like:-
echo $tenMinsFromNow->format('d/m/Y H:i:s');
See it working
PHP version >= 5.4 I'm afraid, but you should be using at least that version by now anyway.
Pakistan, which is the localisation explicitly set, uses "DD-MM-YYYY" format dates so the problem occurs when you cast the date into a string of "MM-DD-YYYY". This American format of date is not parseable by the Pakistan localisation.
If you still want to keep the round-trip to a string and back, use DD-MM-YYYY or the ISO datetime format.
While this is the only (current) answer which actually explains your original issue, I recommend the code be refactored as others have demonstrated.

Categories